【问题标题】:Android ActionBar not changing background colorAndroid ActionBar 不改变背景颜色
【发布时间】:2014-09-01 21:57:16
【问题描述】:

我正在尝试更改操作栏的背景颜色,但它没有改变。如果这会影响任何内容,我的应用正在使用 AppCompat 主题。

这是我正在使用的主题。我的应用指向使用这个主题,所以这不是问题

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/ActionBar</item>
</style>

<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/blue</item>
    <item name="background">@color/blue</item>
</style>

【问题讨论】:

标签: android background android-actionbar themes android-actionbar-compat


【解决方案1】:

在你的 ActionBar 样式中,删除 android:background,只保留 background 属性。还要确保在应用程序标签或扩展 AndroidManifest.xml 文件中的操作栏的活动中实现您的 AppTheme。我还在 Widget.AppCompat 之前添加了“@style/”属性,因为它是 android 库中定义的默认样式。

您的代码应如下所示:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/ActionBar</item>
</style>

<!-- Action Bar Styles -->
<style name="ActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">#0000ff</item>
</style>

【讨论】:

  • 注意同时使用 "android: actionBarStyle" 和 "actionBarStyle" 名称,以便与背景命名空间相同
【解决方案2】:

你也可以从代码中看到这个

ab.setBackgroundDrawable(new ColorDrawable(newcolor));

以防万一您的 xml 不起作用

【讨论】:

  • 我希望它以 XML 形式呈现,这样我就可以在应用程序范围内使用颜色,而且我不必一直将该行添加到每个活动中。
  • 给你 它不起作用的原因之一是样式标签的父级错误
【解决方案3】:

根据此链接,https://developer.android.com/reference/android/R.attr.html#background 它似乎不占用颜色资源,所以你要么给它一个十六进制颜色值,要么创建一个使用你想要的颜色的drawable。您可以尝试以下替代方法,看看它是否有效。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/ActionBar</item>
</style>

<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">#0000ff</item>
    <item name="background">#0000ff</item>
</style>

【讨论】:

  • 使用构建工具 21.1.2,它不允许直接给主题颜色。您必须使用@color@drawable 提供它。
猜你喜欢
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 2013-03-03
  • 2015-08-14
  • 1970-01-01
  • 2013-11-27
  • 2014-03-05
  • 1970-01-01
相关资源
最近更新 更多