首页 > 系统 > Android > 正文

Android 夜间模式的实现(一)

2019-11-08 20:04:39
字体:
来源:转载
供稿:网友

夜间模式实现

 

所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛,实现方式也就是所谓的换肤(主题切换)。对于夜间模式的实现网上流传了很多种方式。也反编译了几个新闻类(你懂得)夜间模式实现的比较的好的App,好歹算是实现了。方式有很多,我现在把我所实现原理(内置主题的方式)分享出来,希望能帮到大家,不喜勿喷(近来笔者小心肝不太安生),有更好的方法也欢迎分享。 

实现夜间模式的时候,我一直纠结下面几个问题

从何处着手。选中夜间模式,如何才能使当前所看到的页面立即呈现出夜间模式的效果又不闪屏其他页面如何设置,特别是在Actionbar上的或者有侧边栏Menu的,比如使用了(actionbar——sherlock)库。

上面的问题咱们先一个一个解决:

 

其一:从何处着手

 

1.1定义属性

 

要想根据主题的不同,设置不同属性,我们至少需要定义下属性的名字吧。要不然系统怎么知道去哪找啊!

定义属性,是在values下进行。

在attrs.xml里定义了几种属性。

[html] view plain copyPRint?在CODE上查看代码片<?xml version="1.0" encoding="utf-8"?>  <resources>      <attr name="colorValue" format="color" />      <attr name="floatValue" format="float" />      <attr name="integerValue" format="integer" />      <attr name="booleanValue" format="boolean" />      <attr name="dimensionValue" format="dimension" />      <attr name="stringValue" format="string" />      <attr name="referenceValue" format="color|reference" />      <attr name="imageValue" format="reference"/>        <attr name="curVisibility">      <enum name="show" value="0" />      <!-- Not displayed, but taken into account during layout (space is left for it). -->      <enum name="inshow" value="1" />      <!-- Completely hidden, as if the view had not been added. -->      <enum name="hide" value="2" />      </attr>  </resources>  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表