2015-05-04 113 views
0

只需更改我的操作欄的顏色!Java android更改操作欄的顏色

所以,當我google一下,它似乎很簡單...

我見過的人提出了好幾次:

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

<!-- ActionBar styles --> 
<style name="MyActionBar" 
    parent="@style/Widget.AppCompat.Light.ActionBar"> 
    <item name="android:background">#990000</item> 
</style> 

但我真的不能得到這個工作。它不會做任何不同的事情。Actionbar仍然是黑色的。 但究竟這似乎是替別人打工,所以我不明白..

當我試圖把這樣的:在我的onCreate

ActionBar actionBar; 

    actionBar = getActionBar(); 
    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#990000")); 
    actionBar.setBackgroundDrawable(colorDrawable); 

,應用程序崩潰只是......

編輯:好吧,我太很快發佈方式白癡... 就不得不把下面的動作條代碼:

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

它現在,對於後遺憾在我嘗試過之前......

+0

您可以從您所遇到的碰撞logcat的發佈日誌? –

+2

@DavidArgyleThacker我幾乎可以保證getActionBar返回null。 –

回答

0

新棒棒糖AppCompat庫不會使用android:命名空間進行主題化。這是極有可能的是你主題化,如果你刪除android:會工作,並做如下所示:

<style name="AppTheme" 
    parent="@style/Theme.AppCompat.Light.DarkActionBar"> 
    <item name="actionBarStyle">@style/MyActionBar</item> 
</style> 

做的更好的辦法是使用Toolbar,而不是過時ActionBar。要做到這一點,你的主題應該來自NoActionBar主題。

<style name="AppTheme" 
    parent="@style/Theme.AppCompat.NoActionBar"> 
</style> 

而且您必須將Toolbar包含到您自己的佈局中,如下所示。這樣你就可以設置你想要的任何背景。

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_actionbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="#990000" /> 
0

應用你的主題,以你的整個應用程序或個別活動的清單:

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