6

我想改變我的操作欄的背景顏色,但它不會改變。我的應用程序使用AppCompat主題,如果這影響任何東西。Android ActionBar不改變背景顏色

這是我正在使用的主題。我的應用程序點使用這個主題,這樣是沒有問題的

<!-- 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> 

+0

你還用** ** AppTheme清單中? – waqaslam 2014-09-01 22:01:43

+0

@waqaslam是。我在這段代碼中做錯了什麼? – 2014-09-01 22:06:10

+1

對於API> 11,缺少AFAIR @ style/ActionBar。有關詳細信息,請參閱https://developer.android.com/training/basics/actionbar/styling.html – 2014-09-01 22:13:11

回答

14

在你的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> 
+1

注意同時使用「** android:** actionBarStyle」和「actionBarStyle」名稱以與背景命名空間兼容 – 2015-12-10 13:09:55

0

您可以SE這從代碼以及

ab.setBackgroundDrawable(新ColorDrawable(newcolor) );

萬一您的XML不工作

+0

我想以XML形式使用它,所以我可以使用顏色應用程序範圍,並且我不必爲每個活動添加該行。 – 2014-09-01 22:12:35

+0

在這裏你去 <樣式名稱= 「MyActionBar」 父= 「@安卓風格/ Widget.Holo.Light.ActionBar」> <項目名稱= 「機器人:背景」> ANY_HEX_COLOR_CODE 其中的一個原因是無法正常工作是因爲對於樣式標記,您的父級錯誤 – Amrut 2014-09-01 22:17:10

+0

仍然沒有工作 – 2014-09-01 22:23:04

0

根據這個鏈接,https://developer.android.com/reference/android/R.attr.html#background 似乎不取色資源,讓你無論是給它一個十六進制顏色值,或創建一個使用顏色繪製你要。你可以嘗試下面的選擇來看看它是否有效。

<!-- 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> 
+0

使用構建工具21.1.2,它不允許直接給主題添加顏色。你*有*使用'@ color'或'@ drawable'給它。 – 2015-07-31 23:06:59