简体   繁体   中英

cannot change color of action bar in android studio

my main activity extendeds AppCompatActivity. i cannot see anything wrong with these codes but my action bar color still doesn't change. help?

styles.xml

<style name="CustomTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/CustomActionBarStyle</item>
</style>

<style name="CustomActionBarStyle" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#008000</item>
</style>

AndroidManifest.xml (i use a splashscreen that's why .MainActivity is not my launcher)

    <activity
        android:name=".MainActivity"
        android:theme="@style/CustomTheme">
        <intent-filter>
            <category android:name="android.intent.category.default" />
        </intent-filter>
    </activity>

Please check this to see which color values you should set/override in your application/activity theme. <item name="android:colorPrimary">@color/primary</item> will be for color of action bar.

Just modify the styles.xml as follow:-

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#FF9800</item> // This will be the color of your Actionbar
    <item name="colorPrimaryDark">#FF9800</item> 
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorButtonNormal">#FF9800</item>
</style>

Try This in your java file instead. Will work on runtime but works 100% irrespective of style used.

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
    actionBar.setBackgroundDrawable(new 
      ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimary)));
}

Don't Forget to import required classes and change 'colorPrimary' with color of your choice.

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