1

i have an application i am working on that has a radioButton group that changes the image background of my main activity. it is working well with one issue i cant seem to solve.

the background always defaults back to the holo background after i switch screens or resart the app. the radio buttons are set up to press properly with a selector xml in drawables and the images switch flawlessly but just don't stick. also how would i spread this across all classes without recreating the radio buttons on every screen?

here is my code for the radio buttons

public class MainActivity extends Activity {

private final String TAG = "Main Activity";

Button rButton2;
Button rButton1;
Button rButton;
Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);             
      final RelativeLayout ll=(RelativeLayout) findViewById(R.id.RelativeLayout);
      rButton2 = (Button) findViewById(R.id.radio2);
      rButton1 = (Button) findViewById(R.id.radio0);
      rButton = (Button) findViewById(R.id.radio1);
      rButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Log.i(TAG, "onStart");
                ll.setBackgroundResource(R.drawable.background1);
          }
        });

      rButton1.setOnClickListener(new OnClickListener() {     

          public void onClick(View v) {
                Log.i(TAG, "onStart");
                ll.setBackgroundResource(R.drawable.background);    
          }
        });

      rButton2.setOnClickListener(new OnClickListener() {     

          public void onClick(View v) {
                Log.i(TAG, "onStart");
                ll.setBackgroundResource(R.drawable.background2);   
          }
        }); 
}
4

1 回答 1

2

Save your preferences First you need to save your settings. Use SharedPreferences for that.

Second you need to set your bakground from code. Either add the initialization code to all your Activities or define a style.

Option1: Initialize background in activities. In the onCreate() of all your activities you call the setBackgroundResource method with the value you retrieve from the SharedPreferences.

Option2: Create themes. Create themes based on the Holo theme of your choice and set it in the onCreate() of your activities. Example for how you can do it is described in this post: How to change current Theme at runtime in Android

Here is a trick for theme switching: How do I restart an Android Activity (You should call recreate())

于 2013-08-30T13:33:24.613 回答