简体   繁体   English

Android使用AppCompat更改操作栏背景颜色

[英]Android change Actionbar background color using AppCompat

I'm trying to change the background color of my ActionBar as well as the title color. 我正在尝试更改ActionBar的背景颜色以及标题颜色。 I must say I am using the support library. 我必须说我正在使用支持库。

My styles are as follows. 我的风格如下。

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
</style>

<style name="AppTheme.ActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="background">@color/red_300</item>
    <item name="android:background">@color/red_300</item>
    <item name="colorPrimary">@color/red_300</item>
    <item name="android:titleTextStyle">@style/AppTheme.ActionBar.Title</item>
</style>

<style name="AppTheme.ActionBar.Title" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red_700</item>
    <item name="android:textColorPrimary">#00FF00</item>
</style>

And in styles-v21 和样式-v21

<style name="AppTheme.ActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="background">@color/red_300</item>
    <item name="android:background">@color/red_300</item>
    <item name="colorPrimary">@color/red_300</item>
    <item name="android:colorPrimary">#FF0000</item>
    <item name="android:colorAccent">#FF0000</item>
    <item name="android:titleTextStyle">@style/AppTheme.ActionBar.Title</item>
</style>

<style name="AppTheme.ActionBar.Title" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:colorPrimary">#FFFF00</item>
    <item name="android:colorAccent">#FFFF00</item>

    <item name="android:textColor">@color/red_700</item>
    <item name="android:textColorPrimary">#00FF00</item>
</style>

As you guys might see by the amount of attemps to change it, I am already losing my mind. 你们可能会发现尝试更改它的次数,但我已经失去了理智。 Any clues as to how I can change the ActionBar background and it's title color? 关于如何更改ActionBar背景及其标题颜色的任何线索?

I must note that I am trying this on Android 6. 我必须注意,我正在Android 6上尝试此操作。

In the ToolBar creation (xml), setting the app:theme fixed it. 在工具栏创建(xml)中,设置app:theme将其修复。 Specifying a theme there with it's colorPrimary changed the background of the ActionBar without affecting the base theme. 在其中指定其主题颜色为colorPrimary,可在不影响基本主题的情况下更改ActionBar的背景。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

and the theme: 和主题:

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM