无法改变动作栏背景

时间:2014-08-06 13:52:51

标签: java android xml android-actionbar

我正在尝试将操作栏的背景颜色更改为纯色,但它不会改变。这是我的代码: /res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@style/Theme.AppCompat.Light.DarkActionBar">

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

<!-- ActionBar styles -->
<style name="MyActionBar"
       parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <!-- Support library compatibility -->
    <item name="android:background">@color/actionbarbg</item>
</style>
</resources>

res/values/styles.xml :(颜色在哪里)

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<color name="actionbarbg">#8793FF</color>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility 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>

/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fragment"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

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

    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.fragment.MainActivity" />
</activity>
</application>

</manifest>

当我更改themes.xml中的父级时,它会应用它,即如果我将其更改为AppCompat.Light,但我不知道如何更改背景颜色。我浏览了互联网和Stack Overflow,没有一个答案解决了我的问题。

1 个答案:

答案 0 :(得分:1)

根据文档 - &#34;您可以使用在Android 3.0(API级别11)中添加的ActionBar API来控制操作栏的行为和可见性。&#34;

因此,ActionBar不适用于API级别为10(Android 2.3.3)的目标环境。

以防万一,如果您的目标是最低API等级11,您可以通过定义自定义样式来更改ActionBar的背景颜色,如下所示:

<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

此处 ANY_HEX_COLOR_CODE 您可以将#0000FF 用于蓝色作为示例

并设置&#34; MyTheme&#34;作为申请/活动的主题。

<application android:theme="@style/MyTheme" 

希望这会有所帮助......