【问题标题】:Setting background color of android actionbar设置android操作栏的背景颜色
【发布时间】:2014-04-05 08:19:39
【问题描述】:

我正在使用兼容库 v7。

我只是想将我的操作栏的颜色(适用于 Android 2.1 及更高版本 - 尽管我运行的是 Android 4.4.2)设置为纯色。

但是颜色不会改变。它保持不变。

我也尝试过用颜色创建一个实体可绘制对象,但这也没有改变。

最后我测试了我是否可以更改布局的背景,并且我可以 - 这一定是关于我没有得到的操作栏背景。

代码如下:

    <?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">

         <item name="android:background">#0000ff</item>  

    </style>
</resources>

【问题讨论】:

    标签: android android-actionbar compatibility android-compatibility


    【解决方案1】:

    这似乎对我有用。尝试使用资源而不是原始值。

    <style name="app_theme" parent="@android:style/Theme.Holo">
        <item name="android:actionBarStyle">@style/app_action_bar</item>
    </style>
    
    <style name="app_action_bar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:background">@color/google_lightest_gray</item>
        <item name="android:icon">@android:color/transparent</item>
        <item name="android:windowActionBarOverlay">false</item>
    </style>
    

    【讨论】:

      【解决方案2】:

      Activity extends SherlockActivity 中使用getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#808080"))); 或您希望的颜色:)

      【讨论】:

        【解决方案3】:

        你可以使用:

        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.colorname));
        

        【讨论】:

          【解决方案4】:

          使用以下代码getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg_color.xml));

          actionbar_bg_color.xml

          <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
                <solid android:color="@color/actionbar_bg"/>  <!-- set desired #0000ff color in color.xml
           you can use here solid color as well as gradient -->
           </shape>
          

          希望对你有帮助

          【讨论】:

            【解决方案5】:

            我解决了这个问题:

            • 正确安装 support-v7-appcompat 库。我的错误。
            • 将样式转移到每个版本的 res/values/styles.xml 文件,即一个用于向后兼容,一个用于 values-v11。

            我不知道为什么我无法在 theme.xml 中得到我想要的结果。

            如果有人回答我将不胜感激

            【讨论】:

            • 对于 AppCompat,我发现您需要在自定义“MyActionBar”样式中使用“background”而不是“android:background”,然后在 Themes.xml 中使用它就可以了。
            猜你喜欢
            • 2015-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-18
            • 1970-01-01
            • 1970-01-01
            • 2013-08-04
            • 1970-01-01
            相关资源
            最近更新 更多