简体   繁体   中英

Android SDK: Can't style ActionBar of support library

For some reason, I can't style action bar using style.xml when I implement support-v7.

I've got two style files. This is style.xml from values folder:

<resources>

    <color name="primary">#009688</color>
    <color name="primaryDark">#00796B</color>

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

    <style name="myActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/primary</item>
    </style>

</resources>

And this lollipop styles from values-v21:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:colorPrimary">@color/primary</item>
        <item name="android:colorPrimaryDark">@color/primaryDark</item>
    </style>
</resources>

Here what happens:

1) My app by default implements android.support.v7.app.ActionBarActivity from support-v7 library. When I start it up:

a) On Android 4.4 action bar is not colored, it's dark just as before.

b) On Android 5 device it's not colored as well, but status bar gets color.

So, action bar styling doesn't work for support-v7's bar. Even if set global textColor parameter to something bright, every text changes color, only title of my app keeps being white.

However, I can still color it in code: getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#673AB7"))) .

2) If I implement android.app.Activity (not android.support.v7.app.ActionBarActivity) for my app:

a) Lollipop device gets neat green action bar and neat green status bar. b) But on 4.4 action bar dissapears completely.

I've tried getting it back with these styling parameters in values/style.xml on the theme tag, but none of them worked:

<item name="android:windowActionBar">true</item>
<item name="android:displayOptions">showTitle</item>

Minimal SDK is set to 15.

What do I do to get styled action bar on both platforms?

Following this guide :

1) All of your Activities must extend from ActionBarActivity, which extends from FragmentActivity from the v4 support library, so you can continue to use fragments.

2) Remove values-v21/themes.xml and leave only values/themes.xml

3) Tell Android to use the Toolbar as ActionBar:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

4) Inside your style.xml, replace

<style name="myActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/primary</item>
</style> 

with

<style name="myActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/primary</item>
</style>

Just set these to your custom base theme in values/style.xml

 <item name="colorPrimary">@color/your_primary_color</item>
 <item name="colorPrimaryDark">@color/your_primary_dark_color</item>
 <item name="colorAccent">@color/your_accent_color</item>
 <item name="windowActionBar">false</item>
 <item name="android:windowActionBar">false</item>
 <item name="android:windowNoTitle">true</item>

And Use android.support.v7.widget.Toolbar . Be sure that you are using support library v21 or later.

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