ActionBar以不同的颜色与不同版本的Android一起出现

时间:2013-10-12 03:13:42

标签: android styles android-actionbar android-actionbar-compat

我知道这个问题看似简单,但之前没有像chei这样的东西。你去吧:

在我的Android应用程序中使用支持库和AppCompat v7,添加了一个ActionBar。一切都很完美,除了当应用程序在android 2.3上运行时,ActionBar的背景是黑暗的,当刮刀android 4.0.2 ActionBar这个背景变灰时。

以下是如何定义我的@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>

我该如何解决这个问题?

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

如果您的最小目标是API级别11,您可以更改背景颜色

<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>

答案 1 :(得分:0)

我想在此问题上为未来的读者添加我的观察结果(使用min sdk 8,目标sdk 19)。

  • 如果你想在不同的Android版本中设置操作栏的背景颜色,那么在代码中更容易做到。

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

这将覆盖版本的默认背景并设置所需的颜色。

注意:我在各种帖子上看过并试图设置背景颜色如下:

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

但它不起作用,暂时不为我所知。所以我成功尝试了上面提到的代码。

  • 如果要设置操作栏的背景颜色(以及其他属性)而不是代码,则可以使用样式和主题。我做了如下样式。
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>
在res / values-v11和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>

注意: 由于我必须在Android版本中有类似的操作栏,我已经从res / values复制粘贴res / values-v11和res / values-v14中的代码。但令我惊讶的是,我没有得到理想的结果。原因?我们需要在android v11及更高版本的标签之前添加“android”命名空间。所以我将 actionBarStyle 更改为 android:actionBarStyle ,将背景更改为 android:background 。这给我带来了理想的结果。