如何更改布局的背景颜色并永远保持该颜色

How to change background colour of a layout and keep that colour for ever

我想做的是在单击按钮时更改布局的颜色,但是我希望该颜色永远是我更改过的颜色。在它所做的那一刻,它将颜色更改为我指定的颜色,但如果我返回或重新打开程序,它会回到正常阶段。

XML 文件;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fffcfdff"
android:weightSum="1">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:id="@+id/linearLayout2">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="381dp"
        android:layout_height="wrap_content"
        android:background="#5DAD68"
        android:id="@+id/toppartofthescreen">

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/backgroundcolour"
            android:layout_below="@+id/linearLayout2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="133dp" />

    </LinearLayout>
</LinearLayout>

这是Java文件;

public class ChangeBackground extends FragmentActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example_layout);

findViewById(R.id.backgroundcolour).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843"));

        }
    });

}
}

例如,您必须在 SharedPreferences 中保留您的颜色,并在您进入应用程序时设置它。如下例所示更新您的代码:

public class ChangeBackground extends FragmentActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.example_layout);
        final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        int color = sharedPreferences.getInt("color", -1);
        if(color != -1){
            findViewById(R.id.toppartofthescreen).setBackgroundColor(color);
        }
        findViewById(R.id.backgroundcolour).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843"));
                sharedPreferences.edit().putInt("color",Color.parseColor("#ffad4843") ).apply();
            }
        });

    }
}

您可以尝试使用 SharedPreferences:

 final SharedPreferences preference  = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
    if(preference.getBoolean("showBgColor", false))
    {
        findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843"));
    }

    findViewById(R.id.backgroundcolour).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!preference.getBoolean("showBgColor", false)) {
                SharedPreferences.Editor editor = preference.edit();
                editor.putBoolean("showBgColor", true);
                editor.commit();
            }
            findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843"));

        }
    });