1

我知道这个问题似乎很简单,但没有像之前的任何东西。所以你去:ActionBar出现在不同的颜色与Android的不同版本

在我的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

+0

是不是可以通过代码将背景可绘制设置为所需的颜色?这样它会保持在任何设备上相同? –

+0

其实通过改变颜色代码的作品是的,但想对它有一个解释 –

回答

2

上分目标这不会为你工作的2.3.3如果你分钟的目标是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> 
+0

它对我来说很好。 但也有在网络上使用此工具来定制您的主题的选项:http://jgilfelt.github.io/android-actionbarstylegenerator/ –

+1

@WalterBarreiroNeto好工具infact。 –

0

我想在这个问题上添加我的观察(使用min sdk 8,target 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); 

,但它没有工作,原因不明,以我的时间being.so我成功尝试上面提到的代码。

  • 如果您想设置不在代码中的操作栏(以及其他属性)的背景色,您可以使用样式和主题。我做了以下样式。

内部RES /值/ 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 /值-V11和RES /值-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-v11中和res/values中的res/values-v14。但令我惊讶的是,我没有得到理想的结果。原因?我们需要在android v11及更高版本的标签之前添加“android”命名空间。因此,我改变actionBarStyle安卓actionBarStyle背景的android:背景。这给我带来了期望的结果。