欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android如何快速適配暗黑模式詳解

 更新時間:2021年08月11日 11:17:55   作者:i小灰  
微信在前段時間的更新中也實現了暗黑模式,而蘋果系統(tǒng)也早就支持暗黑模式,Android也一樣可以實現,下面這篇文章主要給大家介紹了關于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的暗黑樣式。

如何適配

  1. 顏色資源
    新建values-night文件夾,將頁面中使用的色值都替換成暗黑模式下的色值。
  2. 圖片資源
    新建mipmap-night/drawable-night文件夾,將頁面中使用的圖片和樣式資源都替換成暗黑模式下的對應資源。
  3. 狀態(tài)欄
    通過上方代碼中最后一個方法isDarkMode判斷顯示什么顏色的狀態(tài)欄。最好在BaseActivity中操作,否則Activity很多的話很麻煩。
  4. 調用
    調用上方的方法切換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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論