Android深色模式適配
我們需要再用戶設(shè)置時候,記錄下來,用戶的設(shè)置,等app再次啟動時候,獲取之前設(shè)置,重新設(shè)置
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 {
//跟隨系統(tǒng)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
}
1、設(shè)置單個Activity
/**
* 設(shè)置單個Activity 深色、淺色、跟隨系統(tǒng)
*
* @param appCompatDelegate Activity AppCompatDelegate
* @param themeModeType 0:跟隨系統(tǒng) 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設(shè)置
/**
* 設(shè)置webView 深色或者淺色模式
*
* @param activity
* @param webSetting
*/
public static void setWebViewNight(Activity activity, WebSettings webSetting) {
if (activity == null || webSetting == null) {
return;
}
setWebViewNight(webSetting, nightModeByUiResources(activity));
}
/**
* 設(shè)置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 的內(nèi)容將始終以黑暗主題呈現(xiàn)
WebSettingsCompat.setForceDark(webSetting, WebSettingsCompat.FORCE_DARK_ON);
} else {
//禁用 webview 的強制暗模式,這意味著 webview 的內(nèi)容將按原樣呈現(xiàn)
WebSettingsCompat.setForceDark(webSetting, WebSettingsCompat.FORCE_DARK_OFF);
}
}
}
但是h5頁面需要做特別判斷才能拿到webView深淺模式文章來源:http://www.zghlxwxcb.cn/news/detail-811770.html
@media (prefers-color-scheme: dark) {這里是樣式代碼}
4、深色淺色切換時候,重啟app文章來源地址http://www.zghlxwxcb.cn/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());
}
}
到了這里,關(guān)于Android 設(shè)置app深色、淺色、跟隨系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!