2015-12-17 34 views
0

在我的應用程序中,我有3 activities,每個activity具有相同的Toolbar與自定義樣式(與背景顏色藍色)包括在內。所有活動相同的工具欄顏色

Activity-A用戶可以更改toolbar(從藍色到紅色)的背景色。它適用於Activity-Atoolbar,但是當我去Activity-BActivity-Atoolbar的顏色是前一個(背景顏色是藍色不是紅色)。

toolbar是:

<android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:local="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar" 
     android:theme="@style/myToolbarTheme" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:padding="0dp"> 
</android.support.v7.widget.Toolbar> 

自定義樣式:

<style name="myToolbarTheme"> 
    <item name="android:background">@color/themeBlue</item> 
</style> 

更改工具欄的背景顏色我用,

 toolbar.setBackgroundColor(getResources().getColor(R.color.themeRed)); 

所以我的問題是,如何能我在每個活動的工具欄上設置相同的顏色?請建議我。

謝謝

+0

定義樣式普通的工具欄顏色 – KOTIOS

+0

你能解釋一下? –

回答

1

你可以在你的風格用色主

<item name="android:colorPrimary">@color/primary</item> 

enter image description here

更多的參考here

+0

它是否反映了所有「活動」的變化? –

+0

@ User_1191是的,如果您在清單的標記 –

+0

中使用它作爲默認主題,但是接下來我如何以編程方式更改工具欄背景顏色? –

0

我想你應該建立一個更加活動是BaseActivity(擴展Activity
全部你的活動(在這種情況下,3)將延伸BaseActivity
然後你會寫的變化碼工具欄顏色BaseActivity
最後,在每個子活動,呼籲變革的工具欄的顏色代碼BaseActivity

0
<style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/ColorPrimary</item> 
     <item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 
     <item name="colorAccent">#FF4081</item> 
     <item name="android:windowBackground">@color/window_background</item> 
     <item name="android:windowActionBar">false</item> 
     <item name="android:windowNoTitle">true</item> 

    </style> 

適用於工具欄

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_questions_map" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="@style/Theme.DesignDemo" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     android:background="@color/ColorPrimary"> 
    </android.support.v7.widget.Toolbar>