Android深色模式适配
我们需要再用户设置时候,记录下来,用户的设置,等app再次启动时候,获取之前设置,重新设置
public static void setThemeMode() {
int themeModeType = SpUtils.getThemeModeType();
if (themeModeType == 1) {
//1:浅色
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (themeModeType == 2) {
// 2:深色
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
//跟随系统
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
}
1、设置单个Activity
/**
* 设置单个Activity 深色、浅色、跟随系统
*
* @param appCompatDelegate Activity AppCompatDelegate
* @param themeModeType 0:跟随系统 1:浅色 2:深色
*/
public static void setThemeModeByActivity(AppCompatDelegate appCompatDelegate, int themeModeType) {
if (appCompatDelegate != null) {
switch (themeModeType) {
case 0:
appCompatDelegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
break;
case 1:
appCompatDelegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case 2:
appCompatDelegate.setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
}
}
}
2、获取当前Activity是否开启深色
/**
* 通过Activity Resources 获取当前是否开启深色模式
*
* @param object
* @return
*/
public static boolean nightModeByUiResources(Object object) {
if (object != null) {
if (object instanceof Activity) {
int currentNightMode = ((Activity) object).getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
} else if (object instanceof androidx.fragment.app.Fragment) {
int currentNightMode = ((Fragment) object).getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
} else if (object instanceof android.app.Fragment) {
int currentNightMode = ((android.app.Fragment) object).getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
}
}
return false;
}
3、webView设置
/**
* 设置webView 深色或者浅色模式
*
* @param activity
* @param webSetting
*/
public static void setWebViewNight(Activity activity, WebSettings webSetting) {
if (activity == null || webSetting == null) {
return;
}
setWebViewNight(webSetting, nightModeByUiResources(activity));
}
/**
* 设置webView 暗黑模式
*
* @param webSetting
* @param nightMode true:深色 false:浅色
*/
public static void setWebViewNight(WebSettings webSetting, boolean nightMode) {
boolean featureSupported = false;
try {
featureSupported = WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK);
} catch (AbstractMethodError e) {
e.printStackTrace();
}
if (featureSupported) {
if (nightMode) {
//启用 webview 的强制黑暗模式,这意味着 webview 的内容将始终以黑暗主题呈现
WebSettingsCompat.setForceDark(webSetting, WebSettingsCompat.FORCE_DARK_ON);
} else {
//禁用 webview 的强制暗模式,这意味着 webview 的内容将按原样呈现
WebSettingsCompat.setForceDark(webSetting, WebSettingsCompat.FORCE_DARK_OFF);
}
}
}
但是h5页面需要做特别判断才能拿到webView深浅模式文章来源:https://www.toymoban.com/news/detail-811770.html
@media (prefers-color-scheme: dark) {这里是样式代码}
4、深色浅色切换时候,重启app文章来源地址https://www.toymoban.com/news/detail-811770.html
/**
* 浅色和深色模式切换,杀死进程,重新打开app
*
* @param activity
*/
public static void restartApp(Activity activity) {
Intent intent = activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName());
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
activity.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
}
}
到了这里,关于Android 设置app深色、浅色、跟随系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!