首先刚开始 我开始使用了第三方得 Android-skin-support库 因为我的项目是databinding的,升级到最新版本后 库不支持了,所以也是抛弃了,可能是因为这个库的作者工作忙或者是没有时间再去维护这个库了吧,也能理解,大家或者可以用下,也可能会遇到意想不到的问题,反正有利有弊,三思而行
我使用了原生自己切换暗黑模式
第一步 就是创建了一个Model 跟项目解耦开 避免后边改起来印象到主项目 如下图
library_skin 就是暗黑模式的资源
drawable-night 存放shape文件 有的同学放图片也是可以 那你就要创建对应的 比如 drawable-night-xhdpi文件 就是根据分辨放的图片
mipmap-night-xxxhdpi 我这边放的是图片 不用的同学可以略过
values-night 这个下边我存放的是 colors.xml 目前就是这个文件
这些颜色或者资源的名字要和你默认的资源名字或者颜色的名字保持一致 只是文件加名字加了个night
比如 默认
colors.xml
<color name="white">#ffffff</color>
主题model的 values-night colors.xml
<color name="white" >#1B1C24</color>
接下来 就是监听系统切换了暗黑模式 这个我是放在BaseActivity中的 baseApplySkin这个方法就是base的一个回调 子类重写这个方法可以做一些才做
manifest要设置这个属性 你可以在你的主Activity中设置这个属性 没必要在所有的Activity上设置
<activity
android:name=".MainActivity"
android:configChanges ="uiMode"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
tools:ignore="LockedOrientationActivity">
</activity>
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int curr = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
DarkModeUtils.applySystemMode(this);
baseApplySkin(this);
}
DarkModeUtils 切换黑暗模式 已经保存暗黑模式状态
public class DarkModeUtils {
public static final String KEY_CURRENT_MODEL = "night_mode_state_sp";
private static int getNightModel(Context context) {
SharedPreferences sp = context.getSharedPreferences(KEY_CURRENT_MODEL, Context.MODE_PRIVATE);
return sp.getInt(KEY_CURRENT_MODEL, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
public static void setNightModel(Context context, int nightMode) {
SharedPreferences sp = context.getSharedPreferences(KEY_CURRENT_MODEL, Context.MODE_PRIVATE);
sp.edit().putInt(KEY_CURRENT_MODEL, nightMode).apply();
}
/**
* ths method should be called in Application onCreate method
*
* @param application application
*/
public static void init(Application application) {
int nightMode = getNightModel(application);
AppCompatDelegate.setDefaultNightMode(nightMode);
}
/**
* 应用夜间模式
*/
public static void applyNightMode(Context context) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
setNightModel(context, AppCompatDelegate.MODE_NIGHT_YES);
}
/**
* 应用日间模式
*/
public static void applyDayMode(Context context) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setNightModel(context, AppCompatDelegate.MODE_NIGHT_NO);
}
/**
* 跟随系统主题时需要动态切换
*/
public static void applySystemMode(Context context) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
setNightModel(context, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
/**
* 判断App当前是否处于暗黑模式状态
*
* @param context 上下文
* @return 返回
*/
public static boolean isDarkMode(Context context) {
int nightMode = getNightModel(context);
if (nightMode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
int applicationUiMode = context.getResources().getConfiguration().uiMode;
int systemMode = applicationUiMode & Configuration.UI_MODE_NIGHT_MASK;
return systemMode == Configuration.UI_MODE_NIGHT_YES;
} else {
return nightMode == AppCompatDelegate.MODE_NIGHT_YES;
}
}
mainActiviy 重写baseApplySkin
@Override
protected void baseApplySkin(Activity activity) {
super.baseApplySkin(activity);
this.recreate();
LogInputUtil.e(TAG+"执行了几次");
}
main会重新创建 你要保存一些属性 在Activity重建完成一周回复之前用户的操作 比如切换tab的状态 输入框的值
我这里保存的 用户切换的tab 下标
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
//深色模式切换之后 activity会重新创建 记录当前选中的tab
outState.putInt("clickIndex",BaseApplication.clickIndex);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState!=null){
//activity重建以后 获取上次选中的tab
clickIndex=savedInstanceState.getInt("clickIndex");
}
}
下边看下效果
效果完美 使用第三方也有不可控的风险 所以建议自己实现 可控
如果H5的适配暗黑模式的花 去看下我上一篇文章 关于H5适配暗黑的文章H5暗黑模式适配