我想在这个问题上添加我的观察(使用min sdk 8,target sdk 19)以供未来的读者阅读。
这会覆盖不同版本的默认背景并设置所需的颜色。
注:我看到了各种岗位,并试图将背景色设置如下:
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:背景。这给我带来了期望的结果。
是不是可以通过代码将背景可绘制设置为所需的颜色?这样它会保持在任何设备上相同? –
其实通过改变颜色代码的作品是的,但想对它有一个解释 –