一、直接修改widget颜色,这种方式实现起来最简单,但需要每个控件都去修改,太过复杂。例如:

  1. /**
  2. * 相应交互,修改控件颜色
  3. * @param view
  4. */public void onMethod1Click(View view) {
  5. if (view.getId() == R.id.btn_method1) {
  6. int theme = NightModeUtils.getSwitchDayNightMode(this);
  7. NightModeUtils.setBackGroundColor(this, mRootView, theme);
  8. NightModeUtils.setTextColor(this, findViewById(R.id.text), theme);
  9. NightModeUtils.setDayNightMode(this, theme);
  10. }
  11. }

NightModeUitls修改颜色方法

  1. /**
  2. * 修改背景色
  3. * @param context
  4. * @param view
  5. * @param theme
  6. */public static void setBackGroundColor(Context context, View view, int theme) {
  7. int color = context.getResources().getColor(
  8. theme == THEME_SUN ? R.color.light_color : R.color.night_color);
  9. view.setBackgroundColor(color);
  10. }
  11.  
  12. /**
  13. * 修改文字色
  14. * @param context
  15. * @param view
  16. * @param theme
  17. */public static void setTextColor(Context context, View view, int theme) {
  18. int color = context.getResources().getColor(
  19. theme == THEME_SUN ? R.color.night_color : R.color.light_color);
  20. TextView textView = (TextView)view;
  21. textView.setTextColor(color);
  22. }

二、通过修改Theme,更新应用主题。这种方法问题在于,需要重启Activity才能完成界面渲染。

在Activity中调用setContentView之前进行Theme设置:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. NightModeUtils.onActivityCreateSetTheme(this);
  5. setContentView(R.layout.activity_main);
  6. }

NightModeUitls设置Theme方法:

  1. /** Set the theme of the activity, according to the configuration. */public static void onActivityCreateSetTheme(Activity activity) {
  2. int theme = getDayNightMode(activity);
  3. switch (theme) {
  4. case THEME_SUN:
  5. activity.setTheme(R.style.AppSunTheme);
  6. break;
  7. case THEME_NIGHT:
  8. activity.setTheme(R.style.AppNightTheme);
  9. break;
  10. default:
  11. break;
  12. }
  13. }

三、通过怎加一层遮光罩来实现。效果不是很理想。

通过WindowManager,将一个透明背景的TextView加到Activity主界面中。代码如下:

  1. private void night() {
  2. if (mNightView == null) {
  3. mNightView = new TextView(this);
  4. mNightView.setBackgroundColor(0xaa000000);
  5. }
  6.  
  7. WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
  8. LayoutParams.MATCH_PARENT,
  9. LayoutParams.MATCH_PARENT,
  10. WindowManager.LayoutParams.TYPE_APPLICATION,
  11. WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
  12. PixelFormat.TRANSLUCENT);
  13. lp.gravity = Gravity.BOTTOM;
  14. lp.y = 10;
  15.  
  16. try {
  17. mWindowManager.addView(mNightView, lp);
  18. } catch (Exception ex) {
  19. }
  20. }
  21.  
  22. private void day() {
  23. try {
  24. mWindowManager.removeView(mNightView);
  25. } catch (Exception ex) {
  26. }
  27. }

四、最后来看一下Dialog需要怎么实现夜间模式。

AlertDialog.Builder 有一个带style id参数的构造函数,我们就通过这个构造函数来实现Dialog主题的修改,从而达到夜间模式。

  1. public static AlertDialog.Builder createBuilder(Context context) {
  2. if (NightModeUtils.getDayNightMode(context) == NightModeUtils.THEME_SUN) {
  3. return new AlertDialog.Builder(context);
  4. } else {
  5. return new AlertDialog.Builder(context, R.style.NightDialog);
  6. }
  7. }

我们通过如上方法来获取Builder,实现主题切换。其中R.style.NightDialog我采用如下方式:

  1. <style name="NightDialog" parent="android:Theme.Holo.Dialog"><item name="android:windowBackground">@android:color/transparent</item></style>

在android honeycomb之前的版本Theme.Dialog.Alert与AlertDialog这两个style是public的,可以通过修改主题时,重新定义这两个style实现dialog主题的修改,但之后的版本已经将他们开放关闭了。所以,我通过上面的办法实现了dialog的主题修改。

Demo源码

Android夜间模式的几种实现的更多相关文章

  1. Android 高级UI设计笔记23:Android 夜间模式之 两种常用方法(降低屏幕亮度+替换theme)

    1. 夜间模式 所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛.特别是一些新闻类App实现夜间模式是非常人性化的,增强用户体验. 2. 我根据网上的资料 以及自 ...

  2. Android 利用an框架快速实现夜间模式的两种套路

    作者:Bgwan链接:https://zhuanlan.zhihu.com/p/22520818来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 网上看到过大多实现夜间模 ...

  3. android夜间模式实现

    一.概述 android夜间模式实现分为两大类 重启activity的实现 不重启activity的实现 二.正文 1.重启activity实现夜间模式[在界面文件中的实现部分] 1.1在attrs. ...

  4. Android 夜间模式changeskin小结

    @author vivian8725118 @CSDN http://blog.csdn.net/vivian8725118 @简书 http://www.jianshu.com/p/832e9776 ...

  5. Android 高级UI设计笔记24:Android 夜间模式之 WebView 实现白天 / 夜间阅读模式 (使用JavaScript)

    1. 问题引入: 前面我们是使用方法 降低屏幕亮度(不常用) 和 替换theme,两者都是针对Activity的背景进行白天.夜间模式的交换,但是如果我们显示的是Html的内容,这个时候改怎么办? 分 ...

  6. Android 夜间模式的实现

    package com.loaderman.daynightdemo; import android.os.Bundle; import android.support.v7.app.AppCompa ...

  7. Android启动模式(三种)

    1,标准启动模式 通过任务栈,每点一次button,将每一个实例都压入,然后点返回键时候,就弹出之前压入的实例. 每一次的地址都是不同的 测试代码:通过创建一个button和textView来显示本身 ...

  8. Android白天/夜间模式Day/Night Mode标准原生SDK实现

     Android白天/夜间模式Day/Night Mode标准原生SDK实现 章节A:Android实现白天/夜间模式主要控制器在于UiModeManager,UiModeManager是Andr ...

  9. Android 之夜间模式(多主题)的实现

    引言 夜间模式其实属于多主题切换的一种,不过是最麻烦的一种.因为在夜间模式下不仅要切换主色调,次要色调等等,还要覆盖一些特殊的颜色,因为在夜间模式下总不能什么都是黑的把,那不得丑死-.-,所以当你夜间 ...

随机推荐

  1. Codeforces 999F Cards and Joy(二维DP)

    题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个 ...

  2. python 全栈开发,Day25(复习,序列化模块json,pickle,shelve,hashlib模块)

    一.复习 反射 必须会 必须能看懂 必须知道在哪儿用 hasattr getattr setattr delattr内置方法 必须能看懂 能用尽量用__len__ len(obj)的结果依赖于obj. ...

  3. ngrinder的简介与基本使用(转载:https://www.jianshu.com/p/f336180806cc)

    nGrinder简介 nGrinder是基于Grinder开源项目,由NHN公司的开发团队进行了重新设计和完善.nGrinder是一款非常易用,有简洁友好的用户界面和controller-agent分 ...

  4. 《转》CXF WebService 开发文档-目录索引

    CXF WebService 开发文档-目录索引 上次介绍了Axis的WebService,现在我们来看看CXF是怎么完成WebService的. Axis1 WebService 在线博文: htt ...

  5. 单一职责原则(Simple responsibility pinciple, SRP)

    一个类只负责一个功能领域中的相应职责 未完待续

  6. 【莫比乌斯反演】HDU1695_GCD

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 第一道莫比乌斯反演 感觉很巧妙的就是利用了F(x)=(n/x)*(m/x) 之后的那个去重也挺不 ...

  7. window下mongodb安装和配置

    mongodb安装和配置 1.下载:https://www.mongodb.com 2.解压到盘的根目录下,本人解压到D盘根目录 3.在软件根目录下新建一个文件夹data 4.再新建两个文件夹db.l ...

  8. 【值得收藏】一份非常完整的Mysql规范

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 本文从芋道源码转载,在原有内容基础上结合阿里巴巴Java开发手册中Mysql数据库章节的介绍,加上自己的理解和说明,整理而 ...

  9. Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置

    Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...

  10. C# Clone控件

    /// <summary> /// Perform a Clone of the object asdfas. /// </summary> /// <typeparam ...