簡體   English   中英

Android ActionBar不會改變背景顏色

[英]Android ActionBar not changing background color

我正在嘗試更改操作欄的背景顏色,但它沒有改變。 我的應用程序正在使用AppCompat主題,如果它影響任何東西。

這是我正在使用的主題。 我的應用程序指向使用此主題,這不是問題

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

<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/blue</item>
    <item name="background">@color/blue</item>
</style>

在ActionBar樣式中,刪除android:background,僅保留background屬性。 還要確保在應用程序標記或擴展AndroidManifest.xml文件中操作欄的活動中實現AppTheme。 我還在Widget.AppCompat之前添加了“@ style /”屬性,因為它是在android庫中定義的默認樣式。

您的代碼應如下所示:

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

<!-- Action Bar Styles -->
<style name="ActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">#0000ff</item>
</style>

您也可以從代碼中獲取此信息

ab.setBackgroundDrawable(new ColorDrawable(newcolor));

以防您的xml無法正常工作

根據這個鏈接, https://developer.android.com/reference/android/R.attr.html#background它似乎沒有采用顏色資源,所以你要么給它一個十六進制顏色值或創建一個drawable,使用你想要的顏色。 您可以嘗試以下替代方案,看看它是否有效。

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

<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">#0000ff</item>
    <item name="background">#0000ff</item>
</style>

暫無
暫無

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

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