夜间白天切换模式以及沉浸式模式

先来说说沉浸式模式;这里偷懒直接说使用第三方的沉浸式模式:

依赖下面这个库:
/Android沉浸式状态栏SystemBarTint/
compile ‘com.readystatesoftware.systembartint:systembartint:1.0.3’

BaseActivity实现代码:

/**
     *   use SytemBarTintManager
     *   Android沉浸式状态栏SystemBarTint的使用方法
     *   http://blog.csdn.net/hwe_xc/article/details/50553758
     *   @param tintDrawable  自定义传入的图片
     */
    protected void setSystemBarTintDrawable(Drawable tintDrawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager mTintManager = new SystemBarTintManager(this);
            if (tintDrawable != null) {
                mTintManager.setStatusBarTintEnabled(true);
                mTintManager.setNavigationBarTintEnabled(true);
                mTintManager.setTintDrawable(tintDrawable);   // 自定义图片
            } else {
                mTintManager.setStatusBarTintEnabled(false);
                mTintManager.setTintDrawable(null);
            }
        }
    }

    /**
     *   Android沉浸式状态栏SystemBarTint的使用方法
     *   http://blog.csdn.net/hwe_xc/article/details/50553758
     *   @param color  自定义传入的颜色
     */
    public void setSystemBarTintDrawable(String color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager mTintManager = new SystemBarTintManager(this);
            mTintManager.setStatusBarTintEnabled(true);
            mTintManager.setNavigationBarTintEnabled(true);
            mTintManager.setTintColor(Color.parseColor(color));  // 自定义颜色
        }
    }

    /**ndroid沉浸式状态栏SystemBarTint的使用方法*/
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

在activity页面调用: 在onCreate方法中写下面代码就好。

setSystemBarTintDrawable("#24b7a4");

接下来说说夜间白天切换模式:

一般先要在res目录下创建两个values文件夹,一个为values,另一个为选择为夜间模式,
这里写图片描述

然后在values目录下,创建两个colors,一个为正常创建,另一个选择为夜间模式,
这里写图片描述

然后再不同模式下设置不同的颜色,但注意调用的字段是相同的

接下来就是切换类:

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;

import java.lang.ref.WeakReference;


/**
 * Night Mode Helper
 * <p/>
 * <p>Helps use utilise the night and notnight resource qualifiers without
 * being in car or dock mode.
 * <p/>
 * <p>Implementation is simple. Add the follow line at the top of your
 * activity's onCreate just after the super.onCreate(); The idea here
 * is to do it before we create any views. So the new views will use
 * the correct Configuration.
 * <p/>
 * <pre>
 * mNightModeHelper = new NightModeHelper(this, R.style.AppTheme);
 * </pre>
 * <p/>
 * You can now use your instance of NightModeHelper to control which mode
 * you are in. You can choose to persist the current setting and hand
 * it back to this class as the defaultUiMode, otherwise this is done
 * for you automatically.
 * <p/>
 * <p>I'd suggest you setup your Theme as follows:
 * <p/>
 * <ul>
 * <li>
 * <b>res\values\styles.xml</b>
 * <pre>&lt;style name=&quot;AppTheme&quot; parent=&quot;AppBaseTheme&quot;&gt;&lt;/style&gt;</pre>
 * </li>
 * <li>
 * <b>res\values-night\styles.xml</b>
 * <pre>&lt;style name=&quot;AppBaseTheme&quot; parent=&quot;@android:style/Theme.Holo&quot;&gt;&lt;/style&gt;</pre>
 * </li>
 * <li>
 * <b>res\values-notnight\styles.xml</b>
 * <pre>&lt;style name=&quot;AppBaseTheme&quot; parent=&quot;@android:style/Theme.Holo.Light&quot;&gt;&lt;/style&gt;</pre>
 * </li>
 * </ul>
 *
 * @author Simon Lightfoot <simon@demondevelopers.com>
 *     日夜模式切换
 */
public class NightModeHelper {

    private static final String PREF_KEY = "nightModeState";

    private static int sUiNightMode = Configuration.UI_MODE_NIGHT_UNDEFINED;

    private WeakReference<Activity> mActivity;
    private SharedPreferences mPrefs;


    /**
     * Default behaviour is to automatically save the setting and restore it.
     */
    public NightModeHelper(Activity activity) {
        int currentMode = (activity.getResources().getConfiguration()
                .uiMode & Configuration.UI_MODE_NIGHT_MASK);
        mPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
        init(activity, -1, mPrefs.getInt(PREF_KEY, currentMode));
    }

    /**
     * Default behaviour is to automatically save the setting and restore it.
     */
    public NightModeHelper(Activity activity, int theme) {
        int currentMode = (activity.getResources().getConfiguration()
                .uiMode & Configuration.UI_MODE_NIGHT_MASK);
        mPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
        init(activity, theme, mPrefs.getInt(PREF_KEY, currentMode));
    }

    /**
     * If you don't want the autoSave feature and instead want to provide
     * your own persisted storage for the mode, use the defaultUiMode for it.
     */
    public NightModeHelper(Activity activity, int theme, int defaultUiMode) {
        init(activity, theme, defaultUiMode);
    }

    private void init(Activity activity, int theme, int defaultUiMode) {
        mActivity = new WeakReference<Activity>(activity);
        if (sUiNightMode == Configuration.UI_MODE_NIGHT_UNDEFINED) {
            sUiNightMode = defaultUiMode;
        }
        updateConfig(sUiNightMode);

        // This may seem pointless but it forces the Theme to be reloaded
        // with new styles that would change due to new Configuration.
        if (theme != -1) {
            activity.setTheme(theme);
        }
    }

    private void updateConfig(int uiNightMode) {
        Activity activity = mActivity.get();
        if (activity == null) {
            throw new IllegalStateException("Activity went away?");
        }
        Configuration newConfig = new Configuration(activity.getResources().getConfiguration());
        newConfig.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        newConfig.uiMode |= uiNightMode;
        activity.getResources().updateConfiguration(newConfig, null);
        sUiNightMode = uiNightMode;
        if (mPrefs != null) {
            mPrefs.edit()
                    .putInt(PREF_KEY, sUiNightMode)
                    .apply();
        }
    }

    public static int getUiNightMode() {
        return sUiNightMode;
    }

    public void toggle() {
        if (sUiNightMode == Configuration.UI_MODE_NIGHT_YES) {
            notNight();
        } else {
            night();
        }
    }




    public void notNight() {
        updateConfig(Configuration.UI_MODE_NIGHT_NO);
        System.gc();
        System.runFinalization(); // added in https://github.com/android/platform_frameworks_base/commit/6f3a38f3afd79ed6dddcef5c83cb442d6749e2ff
        System.gc();
        mActivity.get().recreate();
    }

    public void night() {
        updateConfig(Configuration.UI_MODE_NIGHT_YES);
        System.gc();
        System.runFinalization(); // added in https://github.com/android/platform_frameworks_base/commit/6f3a38f3afd79ed6dddcef5c83cb442d6749e2ff
        System.gc();
        mActivity.get().recreate();
    }
}

在activity或者fragment中的点击事件中调用方法:

NightModeHelper nightModeHelper = new NightModeHelper(mMainActivity); // 参数为activity

nightModeHelper.toggle();

如果有更好的办法,欢迎留言交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值