2017-08-02 61 views
0

我遇到了更改支持ActionBar的背景可繪製更改大多數欄的顏色但在文本和圖標周圍留下舊顏色的問題。我試着改變了我用來製作它的支持ActionBar和ToolBar的顏色。我已經嘗試了許多不同的UI元素失效方式。我累了設置顏色和文字是不同的命令。我試圖隱藏和顯示文字。我不能讓它變成一種純色。以編程方式更改supportActionBar的顏色

Should be all orange

這裏就是我有我的動作條的風格:

<style name="LocationBar" parent="ThemeOverlay.AppCompat.ActionBar"> 
    <item name="android:textColorPrimary">@color/text_color_primary_inverse</item> 
    <item name="android:textColorSecondary">@color/text_color_primary_inverse</item> 
    <item name="android:background">@color/weather_cool</item> 
</style> 

這是我如何將它添加到我的活動:

<android.support.v7.widget.Toolbar 
    android:id="@+id/location_bar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:theme="@style/LocationBar"/> 

這在Java代碼中,我將其設置爲supportActionBar:

_locationBar = (Toolbar)findViewById(R.id.location_bar); 
setSupportActionBar(_locationBar); 

再經過我取的天氣。我試着調整顏色是這樣的:

ColorDrawable warmDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.weather_warm)); 
getSupportActionBar().setBackgroundDrawable(warmDrawable); 

導致你在圖片中看到。大部分欄變色,但不是全部。

+1

後,你可以分享你的'styles.xml'? – azizbekian

+0

更改主題可以在這裏工作。 –

+0

我添加了很多更多的信息。對不起,這是遲到了,我沒有想到! – CaseyB

回答

1

...改變支撐動作條的背景繪製改變 顏色的大部分酒吧的,但離開舊顏色圍繞文本和 圖標...

出現這種情況的原因是因爲你使用android:theme代替styletheme適用於您的Toolbar中的每個孩子,在這種情況下包括TextView標題背景和您的設置按鈕背景。 style僅用於視圖級別,僅適用於您的Toolbar背景。

<android.support.v7.widget.Toolbar 
    ... 
    android:theme="@style/LocationBar" /> 

以前

<android.support.v7.widget.Toolbar 
    ... 
    style="@style/LocationBar" /> 
0

在您的活動課程中編寫代碼,如下所示。

ActionBar bar = getActionBar(); 
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40"))); 

這將改變背景顏色爲#004D40

+0

這就是我正在做的,並在圖片中得到結果。 – CaseyB

相關問題