Android夜间模式的几种实现
一、直接修改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的主题修改。
Android夜间模式的几种实现的更多相关文章
- Android 高级UI设计笔记23:Android 夜间模式之 两种常用方法(降低屏幕亮度+替换theme)
1. 夜间模式 所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛.特别是一些新闻类App实现夜间模式是非常人性化的,增强用户体验. 2. 我根据网上的资料 以及自 ...
- Android 利用an框架快速实现夜间模式的两种套路
作者:Bgwan链接:https://zhuanlan.zhihu.com/p/22520818来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 网上看到过大多实现夜间模 ...
- android夜间模式实现
一.概述 android夜间模式实现分为两大类 重启activity的实现 不重启activity的实现 二.正文 1.重启activity实现夜间模式[在界面文件中的实现部分] 1.1在attrs. ...
- Android 夜间模式changeskin小结
@author vivian8725118 @CSDN http://blog.csdn.net/vivian8725118 @简书 http://www.jianshu.com/p/832e9776 ...
- Android 高级UI设计笔记24:Android 夜间模式之 WebView 实现白天 / 夜间阅读模式 (使用JavaScript)
1. 问题引入: 前面我们是使用方法 降低屏幕亮度(不常用) 和 替换theme,两者都是针对Activity的背景进行白天.夜间模式的交换,但是如果我们显示的是Html的内容,这个时候改怎么办? 分 ...
- Android 夜间模式的实现
package com.loaderman.daynightdemo; import android.os.Bundle; import android.support.v7.app.AppCompa ...
- Android启动模式(三种)
1,标准启动模式 通过任务栈,每点一次button,将每一个实例都压入,然后点返回键时候,就弹出之前压入的实例. 每一次的地址都是不同的 测试代码:通过创建一个button和textView来显示本身 ...
- Android白天/夜间模式Day/Night Mode标准原生SDK实现
Android白天/夜间模式Day/Night Mode标准原生SDK实现 章节A:Android实现白天/夜间模式主要控制器在于UiModeManager,UiModeManager是Andr ...
- Android 之夜间模式(多主题)的实现
引言 夜间模式其实属于多主题切换的一种,不过是最麻烦的一种.因为在夜间模式下不仅要切换主色调,次要色调等等,还要覆盖一些特殊的颜色,因为在夜间模式下总不能什么都是黑的把,那不得丑死-.-,所以当你夜间 ...
随机推荐
- Codeforces 999F Cards and Joy(二维DP)
题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个 ...
- python 全栈开发,Day25(复习,序列化模块json,pickle,shelve,hashlib模块)
一.复习 反射 必须会 必须能看懂 必须知道在哪儿用 hasattr getattr setattr delattr内置方法 必须能看懂 能用尽量用__len__ len(obj)的结果依赖于obj. ...
- ngrinder的简介与基本使用(转载:https://www.jianshu.com/p/f336180806cc)
nGrinder简介 nGrinder是基于Grinder开源项目,由NHN公司的开发团队进行了重新设计和完善.nGrinder是一款非常易用,有简洁友好的用户界面和controller-agent分 ...
- 《转》CXF WebService 开发文档-目录索引
CXF WebService 开发文档-目录索引 上次介绍了Axis的WebService,现在我们来看看CXF是怎么完成WebService的. Axis1 WebService 在线博文: htt ...
- 单一职责原则(Simple responsibility pinciple, SRP)
一个类只负责一个功能领域中的相应职责 未完待续
- 【莫比乌斯反演】HDU1695_GCD
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 第一道莫比乌斯反演 感觉很巧妙的就是利用了F(x)=(n/x)*(m/x) 之后的那个去重也挺不 ...
- window下mongodb安装和配置
mongodb安装和配置 1.下载:https://www.mongodb.com 2.解压到盘的根目录下,本人解压到D盘根目录 3.在软件根目录下新建一个文件夹data 4.再新建两个文件夹db.l ...
- 【值得收藏】一份非常完整的Mysql规范
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 本文从芋道源码转载,在原有内容基础上结合阿里巴巴Java开发手册中Mysql数据库章节的介绍,加上自己的理解和说明,整理而 ...
- Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...
- C# Clone控件
/// <summary> /// Perform a Clone of the object asdfas. /// </summary> /// <typeparam ...