【问题标题】:Can't change background colour of ActionBar无法更改 ActionBar 的背景颜色
【发布时间】:2014-12-18 21:28:49
【问题描述】:

我正在尝试更改我的 Android 应用的背景颜色,但我无法更改。我搜索了很多帖子,但没有一个解决方案能解决我的问题。

这是我的styles.xml:

<resources>

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

<style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#ff0000</item>
</style>


</resources>

如您所见,我只是想将栏设置为红色。 这是我的清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MenuActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

如您所见,主题设置正确。你可以看到,这是我的 gradle:

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    ...
    minSdkVersion 15
    targetSdkVersion 21



}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

所以,即使这看起来没问题,它并没有将我的 ActionBar 设置为红色。我尝试遵循很多指南(包括官方指南),并且按照他们所说的做了一切。我错过了什么?提前感谢您的耐心等待:)

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    由于您使用的是 AppCompat 库,因此您也应该处理较旧的 android 版本(就像使用 ActionBarSherlock 库时一样):

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="android:actionBarStyle">@style/ActionBar</item>
            <item name="actionBarStyle">@style/ActionBar</item>
        </style>
    
        <style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
            <item name="android:background">@android:color/red</item>
            <item name="background">@android:color/red</item>
        </style>
    </resources>
    

    没有 android 命名空间的项目用于设置旧 android 版本上使用的 appcompat 操作栏的样式。

    请参阅this answer 以获取更多参考。

    您还应该检查一下,您可能有更多针对各种屏幕尺寸和 sdk 版本生成的 styles.xml 文件。

    您可能还想将您的 ActionBar 样式声明添加到这些其他样式中:看起来您的测试设备使用了另一个样式,而不是默认设置。

    【讨论】:

    • 感谢您的快速响应。我只有一个styles.xml,位于“values”文件夹中。我拥有的另一个“values”文件夹是“values-w820dp”,里面只有“dimens.xml”:(
    • 您似乎使用的是较旧的 android 版本进行测试,请参阅我的回答,了解如何继续使其工作
    • 成功了!我必须对您的答案做出的唯一更改是更改 ​​#ff0000 中的颜色它不接受 HEX 作为参数。感谢您的帮助!
    • 很高兴能帮上忙。我将根据您的笔记更新答案,以便其他人也可以从中学习。谢谢!
    • 非常感谢。我搜索了所有网络并尝试了所有解决方案,但只是这项工作。谢谢。
    【解决方案2】:

    <style name="Demo_Theme" parent="Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/actionBG</item>
        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/actionBG</item>
    </style>
    
    <style name="actionBG" parent="Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@color/test_bg_color</item>
        <!-- Support library compatibility -->
        <item name="background">@color/test_bg_color</item>
    </style>
    

    这应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2015-07-25
      • 2015-08-14
      • 2013-03-03
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 2016-10-04
      • 2014-09-25
      相关资源
      最近更新 更多