【问题标题】:Android unable to change background color of ActionBarAndroid 无法更改 ActionBar 的背景颜色
【发布时间】:2015-07-25 11:45:39
【问题描述】:

我正在尝试更改我的操作栏的背景颜色,但在参考了此站点上的 several other 问题后,我仍然无法让他们的解决方案发挥作用。

Styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarStyle">@style/ActionBar</item>
        <item name="android:windowActionBar">true</item>
    </style>

    <style name="ActionBar" parent="Widget.AppCompat.ActionBar.Solid">
        <item name="background">@color/light_blue_menu_bar</item>
    </style>
</resources>

colors.xml

<resources>
    ... 
    <color name="blue_semi_transparent_pressed">#80738ffe</color>
    <color name="light_blue_menu_bar">#87CEFA</color>
</resources>

ma​​nifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

但是 ActionBar 没有出现在我的视图中。我错过了什么?

编辑

解决方案是添加到styles.xml

<item name="android:windowActionBar">true</item>

【问题讨论】:

  • 您的目标是什么版本的 Android?
  • 我的目标是 22,最少 19

标签: android android-actionbar


【解决方案1】:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    <item name="actionBarStyle">@style/ActionBarStyle</item>
    <item name="android:windowActionBar">true</item>
</style>

<style name="ActionBarStyle" parent="Widget.AppCompat.ActionBar">
        <item name="background">@color/blue</item>
    <item name="android:background">@color/blue</item>
</style>

【讨论】:

  • 我更新了我的问题以包含 manifest.xml 的相关部分。我主要使用片段,但我的主要活动确实扩展了 ActionBarActivity。
  • 活动标签主题怎么样?你确定你没有使用 getActionBar().hide();或 requestWindowFeature(Window.FEATURE_NO_TITLE);也可以在 AppTheme 中尝试 true
  • 添加 true 会导致 ActionBar 出现,但它是默认的 Dark 主题,而不是我在样式中配置的浅蓝色。 xml
  • 该更改还会生成对话框“渲染问题。缺少样式。这是所选布局的正确主题吗?使用布局上方的主题组合框选择不同的布局...找不到当前它们中 id 为 0x7fff0002 的样式"
  • 所以我们修复了第一个问题以显示您的操作栏,我将编辑答案以向您展示如何设置颜色的完整示例,
【解决方案2】:

你可以用这个,

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#Color_Code")));

【讨论】:

    【解决方案3】:

    也许您应该在 values-v14 文件夹中编辑 styles.xml。因为您的最低版本是 19。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2013-03-03
      • 2013-11-27
      • 2014-03-05
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      相关资源
      最近更新 更多