其他活动中的SeekBar背景色(Android)

其他活动中的SeekBar背景色(Android),android,xml,colors,background,seekbar,Android,Xml,Colors,Background,Seekbar,目前,我的代码允许我通过seekbar更改主要活动的背景。我将如何在我的所有其他活动中保持这种变化 例如,当我移动搜索杆时,在我的主要活动中背景变为红色,但在第二个活动中背景仍为白色 我如何更新它,使我的所有活动与我的主要活动的颜色相匹配 mainactivity.java public class MainActivity extends ActionBarActivity implements OnSeekBarChangeListener{ RelativeLayo

目前,我的代码允许我通过seekbar更改主要活动的背景。我将如何在我的所有其他活动中保持这种变化

例如,当我移动搜索杆时,在我的主要活动中背景变为红色,但在第二个活动中背景仍为白色

我如何更新它,使我的所有活动与我的主要活动的颜色相匹配

mainactivity.java

   public class MainActivity extends ActionBarActivity implements OnSeekBarChangeListener{

        RelativeLayout myscreen; // SEEKBAR
        SeekBar rsb, gsb, bsb; // SEEKBAR
        int red, green, blue; //SEEKBAR

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

            myscreen=(RelativeLayout)findViewById(R.id.mylayout); //SEEKBAR
            rsb=(SeekBar)findViewById(R.id.RedSeekBar); //SEEKBAR
            gsb=(SeekBar)findViewById(R.id.GreenSeekBar);//SEEKBAR
            bsb=(SeekBar)findViewById(R.id.BlueSeekBar); //SEEKBAR
            rsb.setOnSeekBarChangeListener(this); //SEEKBAR
            gsb.setOnSeekBarChangeListener(this); //SEEKBAR
            bsb.setOnSeekBarChangeListener(this); //SEEKBAR

            Button gotoscreen2 = (Button)findViewById(R.id.BgoToScreen2); //BUTTON
            gotoscreen2.setOnClickListener(new View.OnClickListener() {   //BUTTON
                @Override
                public void onClick(View v) {   //BUTTON
                    // TODO Auto-generated method stub   //BUTTON
                    Intent screen2 = new Intent("com.sunveersinghseera.seekbar.OURSECONDSCREEN");//BUTTON
                    startActivity(screen2);//BUTTON 
                    }
            });
        }

        public void updatebackground(){  //SEEKBAR
            red = rsb.getProgress();  //SEEKBAR
            green = gsb.getProgress();  //SEEKBAR
            blue = bsb.getProgress();  //SEEKBAR
            myscreen.setBackgroundColor(0xff000000+red*0x10000+green*0x100+blue); //SEEKBAR
        }

    Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            updatebackground();
}
secondpage.java

public class OurSecondScreen extends ActionBarActivity {

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

}
}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/mylayout"
    android:background="#FF0000"
    tools:context="com.sunveersinghseera.seekbar.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change the background" />

    <Button
        android:id="@+id/BgoToScreen2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="31dp"
        android:text="Go To Screen 2" />

    <SeekBar
        android:id="@+id/RedSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/GreenSeekBar"
        android:layout_below="@+id/textView1"
        android:max="255"
        android:layout_marginTop="23dp" />

    <SeekBar
        android:id="@+id/GreenSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:max="255"
        android:layout_below="@+id/RedSeekBar" />

    <SeekBar
        android:id="@+id/BlueSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/GreenSeekBar"
        android:max="255"
        android:layout_below="@+id/GreenSeekBar" />
</RelativeLayout>

secondpage.xml

<?xml versio`enter code here`n="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:id="@+id/mylayout"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the second screen" />

</RelativeLayout>


我认为这与secondpage的xml文件有关,但我不太确定。如果有任何帮助,我们将不胜感激。

创建单例变量并保存该变量上的颜色。您将能够从所有活动中访问该变量。在Oncreate方法上添加该颜色。:)您可以将这些值写入SharedReferences,并在其他活动中检索它们。这种方式还可以确保在应用程序被android关闭或关闭后,您的背景色得以保持。