Android如何快速適配暗黑模式詳解
直接上代碼
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); } /** * 跟隨系統(tǒng)主題時需要動態(tài)切換 */ public static void applySystemMode(Context context) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); setNightModel(context, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); } /** * 判斷App當前是否處于暗黑模式狀態(tài) * * @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; } } }
使用流程
很明顯這是切換暗黑模式的方法,在調用前必選先適配App的暗黑樣式。
如何適配
- 顏色資源
新建values-night文件夾,將頁面中使用的色值都替換成暗黑模式下的色值。 - 圖片資源
新建mipmap-night/drawable-night文件夾,將頁面中使用的圖片和樣式資源都替換成暗黑模式下的對應資源。 - 狀態(tài)欄
通過上方代碼中最后一個方法isDarkMode判斷顯示什么顏色的狀態(tài)欄。最好在BaseActivity中操作,否則Activity很多的話很麻煩。 - 調用
調用上方的方法切換App的暗黑模式。需要注意的點如下:
需要注意
androidx上直接調用即可。support上使用并且在Activity中切換暗黑模式,需要動態(tài)調用一下activity.recreate()方法。具體原因看下面源碼:
androidx版本:
/** * Sets the default night mode. This is the default value used for all components, but can * be overridden locally via {@link #setLocalNightMode(int)}. * * <p>This is the primary method to control the DayNight functionality, since it allows * the delegates to avoid unnecessary recreations when possible.</p> * * <p>If this method is called after any host components with attached * {@link AppCompatDelegate}s have been 'started', a {@code uiMode} configuration change * will occur in each. This may result in those components being recreated, depending * on their manifest configuration.</p> * * <p>Defaults to {@link #MODE_NIGHT_FOLLOW_SYSTEM}.</p> * * @see #setLocalNightMode(int) * @see #getDefaultNightMode() */ public static void setDefaultNightMode(@NightMode int mode) { switch (mode) { case MODE_NIGHT_NO: case MODE_NIGHT_YES: case MODE_NIGHT_FOLLOW_SYSTEM: case MODE_NIGHT_AUTO_TIME: case MODE_NIGHT_AUTO_BATTERY: if (sDefaultNightMode != mode) { sDefaultNightMode = mode; applyDayNightToActiveDelegates(); } break; default: Log.d(TAG, "setDefaultNightMode() called with an unknown mode"); break; } }
support版本:
public static void setDefaultNightMode(int mode) { switch(mode) { case -1: case 0: case 1: case 2: sDefaultNightMode = mode; break; default: Log.d("AppCompatDelegate", "setDefaultNightMode() called with an unknown mode"); } }
對比后可以發(fā)現androidx切換暗黑模式后,自己主動調用了apply方法,使Activity重建。而support上沒有,僅僅是賦值而已。所以support版本上使用需要自己調用activity.recreate()方法。
總結
到此這篇關于Android如何快速適配暗黑模式的文章就介紹到這了,更多相關Android適配暗黑模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android常用的AlertDialog對話框及自定義對話框
本文主要介紹了android常用的AlertDialog對話框及自定義對話框的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04android 為應用程序創(chuàng)建桌面快捷方式技巧分享
手機裝的軟件過多,找起來很不方便,所以在主頁面有一個快捷方式的話會很不錯的,本文將介紹如何實現,需要了解跟多的朋友可以參考下2012-12-12Android 出現問題Installation error: INSTALL_FAILED_CONFLICTING_P
這篇文章主要介紹了Android 出現問題Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER解決辦法的相關資料,需要的朋友可以參考下2016-12-12Android開發(fā)實現高仿優(yōu)酷的客戶端圖片左右滑動切換功能實例【附源碼下載】
這篇文章主要介紹了Android開發(fā)實現高仿優(yōu)酷的客戶端圖片左右滑動切換功能,結合實例形式分析了Android基于ViewPager實現圖片切換效果的相關操作技巧,并附帶完整工程源碼供讀者下載參考,需要的朋友可以參考下2017-11-11