ActionBar appearing in different colors with diffe

2019-08-12 04:47发布

I know the question seems simple, but not anything like chei here before. So there you go:

In my android application using the Support Library with AppCompat v7, added an ActionBar. Everything works perfectly, except that when the application runs on android 2.3, the background of the ActionBar is dark, and when the squeegee android 4.0.2 ActionBar this background turns gray.

Below is how to define my @style

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

how can I solve this?

enter image description here enter image description here

2条回答
孤傲高冷的网名
2楼-- · 2019-08-12 05:15

This will not work for you on min target as 2.3.3 in case your min target is API level 11 you can change the background color

<resources>
    <style name="AppTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>
查看更多
beautiful°
3楼-- · 2019-08-12 05:32

I would like to add my observation (using min sdk 8, target sdk 19) for the future readers on this issue.

  • If you want to set just the background color of the action bar across different android versions, its easier to do it in code.

    actionBar = getSupportActionBar(); actionBar.setBackgroundDrawable( getResources().getDrawable(R.color.your_desired_color));

This overrides the default background across versions and set your desired color.

Note: I had seen on various post and tried to set the background color as follows:

ColorDrawable colorDrawable = new ColorDrawable();
colorDrawable.setColor(R.color.your_desired_color);
actionBar.setBackgroundDrawable(colorDrawable);

but it did not work, reason unknown to me for the time being.so I successfully tried the code mentioned above.

  • if you want to set the background color of the action bar (along with other attributes) not in code, you can use style and theme. I had done the styling as follows.

inside res/values/style.xml

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 11 theme customizations can go here. -->
    </style>

     <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

   <!-- custom theme for my app -->
    <style name="MyAppTheme" parent="AppTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

     <!-- ActionBar styles -->
    <style name="MyActionBar"  parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
         <item name="android:background">@color/bgcolor</item>
    </style>

</resources>

inside res/values-v11 and res/values-v14

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 11 theme customizations can go here. -->
    </style>

     <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

   <!-- custom theme for my app -->
    <style name="MyAppTheme" parent="AppTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

     <!-- ActionBar styles -->
    <style name="MyActionBar"  parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
         <item name="android:background">@color/bgcolor</item>
    </style>

</resources>

Note: As I had to have similar look for the action bar across android versions, I had copy-pasted the code in res/values-v11 and res/values-v14 from res/values. But to my surprise,I did not get the desired result. Reason? We need to add the "android" namespace before the tags for android v11 and later. So I changed actionBarStyle to android:actionBarStyle and background to android:background. That brought me the desired result.

查看更多
登录 后发表回答