1.定義主體顏色:android
在style自定義了三個屬性:app
<item name="textLight">@android:color/white</item> <item name="appbg">@color/colorPrimaryDarkNight</item> <item name="textNight">@color/gray</item>
他們並非android自由的屬性,他們是我自定義的屬性。他們的定義寫在attrs.xml裏面code
<resources> <attr name="textLight" format="reference" /> <attr name="textNight" format="reference" /> <attr name="appBg" format="reference" /> </resources>
在這裏定義事後,就能夠在xml中直接使用,只須要引用進去就好
eg. android:background="?attr/appbg"orm
這樣背景色就自動轉換成了你設置的這個屬性值。xml
2.設置主題顏色get
activity 提供了一個方法 setTheme(int id) ,可是要注意這個要在 setContentView(int id) 以前調用。就是說咱們要在視圖顯示以前就把顏色切換掉。也就是須要重啓當前的activity。
除此以外,以前渲染過的視圖也須要進行處理,也就是處理那些 「過期」 的 activity。我這裏用的方法是清空咱們的 back stack。it
final Intent themeintent = getIntent(); themeintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(themeintent);
FLAG_ACTIVITY_NEW_TASK 是爲要啓動的activity新建一個任務堆棧。
FLAG_ACTIVITY_CLEAR_TASK 任何放置該activity的已存在的task裏面 的activity都被清空。也就是清空過期的activity。form