前言
今天读到Theme主题实作,刚好又是app主题色系变化。这次採用setTheme和Style来切换。
正文
利用setTheme的话,必须先在styles.xml档案里先做更新<resources> <!-- 原本的 --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- 我新增的 --> <style name="AppTheme.Translucent" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="android:windowBackground">@color/pink</item> <item name="android:colorForeground">@color/white</item> <item name="android:colorBackground">@color/pink</item> </style></resources>
onCreate@Override protected void onCreate(Bundle savedInstanceState) { //一定要打在super.onCreate和setContentView的上面 setTheme(R.style.AppTheme_Translucent); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
结果

结论
这次利用setTheme的方法直接做切换的也是写死的,不太能用spinner和button做切换(我菜)。但像DAY1那篇用layout就必须重複多做几次不同色调layout,东西一多工程量也挺大的,一改也要动全身。如过要能活动式必须要利用animation来辅助,不过那就是另外一个世界了,等之后练习到animation再来试试看。