夜间模式 values-night
来源:互联网 发布:mac能玩守望先锋吗 编辑:程序博客网 时间:2024/05/01 10:53
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> * Helps use utilise the night and notnight resource qualifiers without being in car or dock mode. * <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. * * <pre> * mNightModeHelper = new NightModeHelper(this, R.style.AppTheme); * </pre> * * 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> * I'd suggest you setup your Theme as follows: * <ul> * <li><b>res\values\styles.xml</b> * * <pre> * <style name="AppTheme" parent="AppBaseTheme"></style> * </pre> * * </li> * <li><b>res\values-night\styles.xml</b> * * <pre> * <style name="AppBaseTheme" parent="@android:style/Theme.Holo"></style> * </pre> * * </li> * <li><b>res\values-notnight\styles.xml</b> * * <pre> * <style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light"></style> * </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 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); } public static int getUiNightMode() { return sUiNightMode; } 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. 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 void toggle() { if (sUiNightMode == Configuration.UI_MODE_NIGHT_YES) { notNight(); } else { night(); } } public void notNight() { updateConfig(Configuration.UI_MODE_NIGHT_NO); mActivity.get().recreate(); } public void night() { updateConfig(Configuration.UI_MODE_NIGHT_YES); mActivity.get().recreate(); }}
转自https://gist.github.com/slightfoot/c508cdc8828a478572e0
0 0
- 夜间模式 values-night
- Android白天/夜间模式Day/Night Mode标准原生SDK实现
- 如何在Ubuntu 16.04,Ubuntu 15.10 中安装 f.lux(f.lux支持Night Shift夜间模式)
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 示例1.3 文件过滤器
- makefie 之赋值
- 即时通信
- linux配置SSH协议免登录密码
- 示例1.4 字节流输出
- 夜间模式 values-night
- Web 服务器错误代码
- http://www.cnblogs.com/technology/p/4467895.html
- 解决重新安装sqlserver2008报错Reporting Services目录数据库文件存在的问题
- 套接字—Socket
- 示例1.5 字符流输出
- Android Camera HAL3中预览preview模式下的控制流
- 示例1.6 文件拷贝
- 安卓手把手教你实现RecycleView横向和ViewPage的级联滑动