简体   繁体   中英

Android - Set ActionBar color across all Activities?

How can I change the color of the ActionBar across all activities? For example, I set the actionbar color in my SettingsActivity, then want to apply that to my MainActivity.

I'm using this code to set the color: actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));

I'm also trying to use SharedPreferences to do this, which I haven't found to work so far: prefs.edit().putInt("com.matt.cards.app", actionBarColor); And to get the ActionBarColor: int isActionBar = prefs.getInt("com.matt.cards.app", 0);

I don't really know how SharedPrefs work, so can anyone guide me? Or show me another way to do this?

Thanks

You should create a class that all your activity extends ... for example ...

public class MyActionBarActivity extends Activity {

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setBackgroundDrawable(new ColorDrawable(getResources()
                .getColor(R.color.white_background)));
    }

}

Then let all of your custom activies extends this class like

public class Activity1 extends  MyActionBarActivity 

public class Activity2 extends  MyActionBarActivity 

public class Activity3 extends  MyActionBarActivity 

etc.

try like this hope it will help you,

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarStyle">@style/ActionBarTheme</item>

    </style>

    <style name="ActionBarTheme" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">yourcolor</item>
    </style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM