1. array.xml 中设置变量
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="toolbarBackground" format="reference|color"/>
<attr name="mainEditBackground" format="reference"/>
<attr name="searchEditBackground" format="reference"/>
<attr name="searchStartBackground" format="reference"/>
<attr name="tabLayoutBackground" format="reference|color"/>
<attr name="fragmentBackground" format="reference|color"/>
<attr name="recyclerBackground" format="reference|color"/>
<attr name="recyclerItemBackground" format="reference|color"/>
<attr name="recyclerTextColor" format="reference|color"/>
<attr name="fabBackground" format="reference|color"/>
<attr name="navigationBackground" format="reference|color"/>
<attr name="navigationTextColor" format="reference|color"/>
<attr name="PreferenceBackground" format="reference|color"/>
<attr name="nightModeSwitch" format="reference"/>
</resources>
2. style.xml 中创建白天和夜间模式的主题,分别设置变量的值
<style name="MyTheme.NoActionBar.Day" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/purple</item>
<item name="colorPrimaryDark">@color/purple</item>
<item name="colorAccent">@color/yellow</item>
<item name="toolbarBackground">@color/purple</item>
<item name="mainEditBackground">@drawable/search_day_selector_main</item>
<item name="searchEditBackground">@drawable/search_day_selector_search</item>
<item name="searchStartBackground">@drawable/search_start_day_selector</item>
<item name="tabLayoutBackground">@color/purple</item>
<item name="fragmentBackground">@color/gray_background</item>
<item name="recyclerBackground">@color/gray_background</item>
<item name="recyclerItemBackground">@drawable/title_day_selector</item>
<item name="recyclerTextColor">@color/black_title_text</item>
<item name="fabBackground">@color/yellow</item>
<item name="navigationBackground">@color/white</item>
<item name="navigationTextColor">@color/black_title_text</item>
<item name="PreferenceBackground">@color/white</item>
<item name="nightModeSwitch">@drawable/night_mode</item>
</style>
<style name="MyTheme.NoActionBar.Night" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/black_night</item>
<item name="colorPrimaryDark">@color/black_night</item>
<item name="colorAccent">@color/gray_night</item>
<item name="toolbarBackground">@color/black_night</item>
<item name="mainEditBackground">@drawable/search_night_selector_main</item>
<item name="searchEditBackground">@drawable/search_night_selector_search</item>
<item name="searchStartBackground">@drawable/search_start_night_selector</item>
<item name="tabLayoutBackground">@color/gray_night</item>
<item name="fragmentBackground">@color/black_night</item>
<item name="recyclerBackground">@color/black_night</item>
<item name="recyclerItemBackground">@drawable/title_night_selector</item>
<item name="recyclerTextColor">@color/white</item>
<item name="fabBackground">@color/gray_night</item>
<item name="navigationBackground">@color/gray_night</item>
<item name="navigationTextColor">@color/white</item>
<item name="PreferenceBackground">@color/gray_search_history</item>
<item name="nightModeSwitch">@drawable/day_mode</item>
</style>
3. 布局里用 ?attr/...... 获取当前主题的相应变量的值
如:
......
android:background="?attr/fragmentBackground"
......
4. 活动中 setTheme() 设置不同主题,注意要在 onCreate() 前
-
一般用一个 BaseActivity 作其他活动的父类:
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this);
if (pref.getBoolean("isNightMode",false)){
setTheme(R.style.MyTheme_NoActionBar_Night);
}else {
setTheme(R.style.MyTheme_NoActionBar_Day);
}
super.onCreate(savedInstanceState);
}
}
-
在活动中 setTheme() 后要 reCreate(),但这样会闪屏。
5. 解决闪屏的几种可行方法
- 不用 setTheme(),直接给视图改变背景色等。
- 添加一个 View 挡在活动上面,setTheme() 结束后移除 View。
- View 的背景可以设置成昼夜转换图片:
nightModeView=new View(this);
nightModeView.setBackgroundResource(R.drawable.night);
WindowManager.LayoutParams params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(nightModeView,params);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recreate();
}
}, 100);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
windowManager.removeViewImmediate(nightModeView);
}
}, 1000);
- 或使用转换前的屏幕截图做背景:
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap=view.getDrawingCache();
BitmapDrawable bd=new BitmapDrawable(null,bitmap);
nightModeView=new View(this);
nightModeView.setBackground(bd);
WindowManager.LayoutParams params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(nightModeView,params);
AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(1000);
nightModeView.startAnimation(alphaAnimation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
windowManager.removeViewImmediate(nightModeView);
view.setDrawingCacheEnabled(false);
}
},1000);
截图做背景不能用在要 reCreate() 的时候,一般用来在直接设置视图背景的情况下,同时设置 alpha 变化,使得转换很自然。