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

    /**
* 相应交互,修改控件颜色
* @param view
*/public void onMethod1Click(View view) {
if (view.getId() == R.id.btn_method1) {
int theme = NightModeUtils.getSwitchDayNightMode(this);
NightModeUtils.setBackGroundColor(this, mRootView, theme);
NightModeUtils.setTextColor(this, findViewById(R.id.text), theme);
NightModeUtils.setDayNightMode(this, theme);
}
}

NightModeUitls修改颜色方法

    /**
* 修改背景色
* @param context
* @param view
* @param theme
*/public static void setBackGroundColor(Context context, View view, int theme) {
int color = context.getResources().getColor(
theme == THEME_SUN ? R.color.light_color : R.color.night_color);
view.setBackgroundColor(color);
} /**
* 修改文字色
* @param context
* @param view
* @param theme
*/public static void setTextColor(Context context, View view, int theme) {
int color = context.getResources().getColor(
theme == THEME_SUN ? R.color.night_color : R.color.light_color);
TextView textView = (TextView)view;
textView.setTextColor(color);
}

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

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

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NightModeUtils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_main);
}

NightModeUitls设置Theme方法:

    /** Set the theme of the activity, according to the configuration. */public static void onActivityCreateSetTheme(Activity activity) {
int theme = getDayNightMode(activity);
switch (theme) {
case THEME_SUN:
activity.setTheme(R.style.AppSunTheme);
break;
case THEME_NIGHT:
activity.setTheme(R.style.AppNightTheme);
break;
default:
break;
}
}

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

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

    private void night() {
if (mNightView == null) {
mNightView = new TextView(this);
mNightView.setBackgroundColor(0xaa000000);
} WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
lp.gravity = Gravity.BOTTOM;
lp.y = 10; try {
mWindowManager.addView(mNightView, lp);
} catch (Exception ex) {
}
} private void day() {
try {
mWindowManager.removeView(mNightView);
} catch (Exception ex) {
}
}

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

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

    public static AlertDialog.Builder createBuilder(Context context) {
if (NightModeUtils.getDayNightMode(context) == NightModeUtils.THEME_SUN) {
return new AlertDialog.Builder(context);
} else {
return new AlertDialog.Builder(context, R.style.NightDialog);
}
}

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

    <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. Laravel框架中Blade模板的用法

    1. 继承.片段.占位.组件.插槽 1.1 继承 1.定义父模板 Laravel/resources/views/base.blade.php 2.子模板继承 @extends('base') 1.2 ...

  2. SqlServer基础语法(三)

    1.数据库备份的方法: 完整数据库备份GPOSDB 文件大小:23MB 日志备份 GPOSDB日志备份文件大小:211KB --完整备份 Backup DATABASE GPOSDB To disk= ...

  3. 192 Word Frequency

    Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...

  4. 这篇文章讲得精彩-深入理解 Python 异步编程(上)!

    可惜,二和三现在还没有出来~ ~~~~~~~~~~~~~~~~~~~~~~~~~ http://python.jobbole.com/88291/ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  5. textarea 标签换行及靠左

    上图可以实现文字文字靠左,换行直接回车就行,效果见下图

  6. bind this指针

    var TEST = { msg: 'test', ping: function() { return this.msg }.bind(this /* this -> window */) }

  7. [转]硬盘的那些事(主分区、扩展分区、逻辑分区、活动分区、系统分区、启动分区、引导扇区、MBR等)

    http://xu3stones.blog.163.com/blog/static/205957136201210309424303 主分区,扩展分区,逻辑分区,活动分区,系统分区,启动分区..... ...

  8. [转]一个研究生毕业以后的人生规划[ZZ]

    只有选择去国内的大公司或外企才是出路 文章转载如下: 我今年39岁了, 25岁研究生毕业,工作14年,回头看看,应该说走了不少的弯路,有一些经验和教训.现在开一个小公司,赚的钱刚够养家糊口的.看看这些 ...

  9. VS2010发布、打包安装程序(超全超详细)

    1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1.“应 ...

  10. 在vim中注释多行

    使用查找替换的方法 在linux中,文本每一行的起始标志是^,结束标志为$,因此使用vim搜索^并替换为^#即可. :10,20s/^/#/g 表示将10-20行添加注释,同理取消注释为: :10,2 ...