Android ActionBar后台更改不起作用

时间:2015-03-06 23:11:12

标签: android

我正在尝试更改我的应用中操作栏的背景颜色,而我的styles.xml看起来:

<resources>

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">

    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->

    <!--   darker variant for the status bar and contextual app bars -->
    <item name="colorPrimary">#71343e</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <!--   native widgets will now be "tinted" with accent color -->
    <item name="colorAccent">#ff71bf41</item>

    <!--Action bar style-->
    <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
    <item name="actionBarStyle">@style/AppTheme.ActionBar</item>

</style>

<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/AppTheme.ActionBar.TitleText</item>
    <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleText</item>
    <item name="android:height">100dp</item>
    <item name="android:colorBackground">#71343e</item>

</style>

<style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">#0d5875</item>
</style>

我已尝试使用各种颜色参数,但任何人都可以使用。如何更改ActionBar颜色?

1 个答案:

答案 0 :(得分:0)

对于 android version 3.0 + ,这是一个解决方案:

res/values/themes.xml内:

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

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>
</resources>

并将其设置为整个应用程序(或个人活动)

<application android:theme="@style/CustomActionBarTheme" ... />

对于 android版本2.1 + ,您必须稍微修改一下

<?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">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

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

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_background</item>
    </style>
</resources>

<application android:theme="@style/CustomActionBarTheme" ... />

您可以找到有关样式化ActionBar here

的更多信息