簡體   English   中英

使用AppCompat更改操作欄的背景顏色

[英]Change Background color of the action bar using AppCompat

我在網上發現了有關此問題的一些問題。 不幸的是,到目前為止,我嘗試的所有操作均未成功。

標題說,我需要更改操作欄的背景顏色。

該項目的最小sdk為9,最大sdk為19。

我在res / values文件夾中創建了一個xml文件:

red_actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@color/red</item>
    </style>
</resources>

存儲在res / values中的colors.xml

<resources>
    <color name="red">#FF0000</color>
</resources>

和清單中我更改主題的部分

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >

但是什么都沒有改變。 問題出在哪兒? 該應用程序接受該代碼,因為如果我更改以下內容:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar ">

它確實改變了我的應用程序的主題,所以問題出在風格上,但我不知道如何解決。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background"  tools:ignore="NewApi">@color/red</item>
        <item name="background">@color/red</item>
    </style>
</resources>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background"  tools:ignore="NewApi">@color/red</item>
    <item name="background">@color/red</item>
</style>

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle"   tools:ignore="NewApi">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

</style>

您需要android:backgroundbackground項目。 前者適用於原生支持ActionBar的較新版本的android。 后者適用於較早的android版本。

編輯

代替<resources>使用

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

從SDK 21

要更改操作欄背景顏色,請將其添加到ActionBarTheme中,這兩種顏色將有所不同或將不起作用(感謝@Dre@lagoman

<item name="colorPrimary">@color/my_awesome_red</item> 
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>

嘗試這個:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

</style>

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

    <item name="android:background">@color/red</item>
    <item name="background">@color/red</item>

</style>

嘗試這個。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary"> #6699FF </item>
</style>

您可以根據自己的選擇更改#6699FF顏色(或使用顏色資源)。

嘗試使用

parent="Theme.AppCompat.Light"

代替

parent="@style/Theme.AppCompat.Light"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM