Android 夜间模式的实现代码示例

夜间模式实现

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

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

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

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

其一:从何处着手

1.1定义属性

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

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

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

<?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> 

从上面的xml文件的内容可以看到,attr里可以定义各种属性类型,如color、float、integer、boolean、dimension(sp、dp/dip、px、pt...)、reference(指向本地资源),还有curVisibility是枚举属性,对应view的invisibility、visibility、gone。

1.2定义主题

接着,我们需要在资源文件中定义若干套主题。并且在主题中设置各个属性的值。

本例中,我在styles.xml里定义了DayTheme与NightTheme。

<style name="DayTheme" parent="Theme.Sherlock.Light">>
  <item name="colorValue">@color/title</item>
  <item name="floatValue">0.35</item>
  <item name="integerValue">33</item>
  <item name="booleanValue">true</item>
  <item name="dimensionValue">16dp</item>
  <!-- 如果string类型不是填的引用而是直接放一个字符串,在布局文件中使用正常,但代码里获取的就有问题 -->
  <item name="stringValue">@string/action_settings</item>
  <item name="referenceValue">@drawable/bg</item>
  <item name="imageValue">@drawable/launcher_icon</item>
  <item name="curVisibility">show</item>
</style>
<style name="NightTheme" parent="Theme.Sherlock.Light">
  <item name="colorValue">@color/night_title</item>
  <item name="floatValue">1.44</item>
  <item name="integerValue">55</item>
  <item name="booleanValue">false</item>
  <item name="dimensionValue">18sp</item>
  <item name="stringValue">@string/night_action_settings</item>
  <item name="referenceValue">@drawable/night_bg</item>
  <item name="imageValue">@drawable/night_launcher_icon</item>
  <item name="curVisibility">hide</item>
</style>

1.3在布局文件中使用

定义好了属性,我们接下来就要在布局文件中使用了。

为了使用主题中的属性来配置界面,我定义了一个名为setting.xml布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="?attr/referenceValue"
  android:orientation="vertical"
  >
  <TextView
    android:id="@+id/setting_Color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textColor="?attr/colorValue" />
  <CheckBox
        android:id="@+id/setting_show_answer_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="?attr/booleanValue"/>
  <TextView
    android:id="@+id/setting_Title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="?attr/dimensionValue"
    android:text="@string/text_title"
    android:textColor="?attr/colorValue" />
  <TextView
    android:id="@+id/setting_Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="?attr/stringValue" /> 

  <ImageView
    android:id="@+id/setting_Image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="?attr/imageValue" /> 

  <View android:id="@+id/setting_line"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:visibility="?attr/curVisibility"
    />
</LinearLayout>

从这个布局文件中可以看到,通过“?attr/……” 格式来引用主题中的值,包括(字符串、图片、bool类型、尺寸设置等)。

1.4设置主题及布局文件

布局文件与主题都写好了,接下来我们就要在Activity的onCreate方法里使用了。

大致应该像这样子的:

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(MyApplication.appConfig.getNightModeSwitch()){
      this.setTheme(R.style.NightTheme);
    }else{
      this.setTheme(R.style.DayTheme);
    }
    setContentView(R.layout.setting);
    ……
  }

ps:

  • MyApplication.appConfig.getNightModeSwitch()//是获取pf中当前所处的模式。
  • 一定要放到setContentView();方法之前设置。

如果你使用的fragment 大致应该像下面的样子:

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
   if(MyApplication.appConfig.getNightModeSwitch()){
     getActivity().setTheme(R.style.NightTheme);
   }else{
     getActivity().setTheme(R.style.DayTheme);
   }
   final View view = inflater.inflate(R.layout.setting, null);
   …… 

  } 

ps:建议放到onCreateView(……)方法里面。

值得注意的是,要是默认主题里没那些属性,解析布局文件时候是会crash。这点在配置多个不同style时要主题时,属性可以多,但一定不能少。

比如在attrs.xml文件中

<item name="floatValue">1.44</item>
<item name="integerValue">55</item> 

这两个属性没有用到,但却没有问题。

如果按照上面的操作完毕之后,程序运行起来应该就会看到效果了

那第二个问题呢?

直接看源码

源码地址:NightModel_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现夜间模式切换功能实现代码

    现在很多App都有夜间模式,特别是阅读类的App,夜间模式现在已经是阅读类App的标配了,事实上,日间模式与夜间模式就是给App定义并应用两套不同颜色的主题,用户可以自动或者手动的开启,今天用Android自带的support包来实现夜间模式.由于Support Library在23.2.0的版本中才添加了Theme.AppCompat.DayNight主题,所以依赖的版本必须是高于23.2.0的,并且,这个特性支持的最低SDK版本为14,所以,需要兼容Android 4.0的设备,是不能使用这

  • Android主题切换之探究白天和夜间模式

    智能手机的迅速普及,大大的丰富了我们的娱乐生活.现在大家都喜欢晚上睡觉前玩会儿手机,但是应用的日间模式往往亮度太大,对眼睛有较为严重的伤害.因此,如今的应用往往开发了 日间和夜间 两种模式供用户切换使用,那日间和夜间模式切换究竟是怎样实现的呢? 在文字类的App上面基本上都会涉及到夜间模式.就是能够根据不同的设定.呈现不同风格的界面给用户.而且晚上看着不伤眼睛.实现方式也就是所谓的换肤(主题切换).对于夜间模式的实现网上流传了很多种方式.这里先分享一个方法给大家.通过设置背景为透明的方法.降低屏

  • Android 实现夜间模式的快速简单方法实例详解

    ChangeMode 项目地址:ChangeMode Implementation of night mode for Android. 用最简单的方式实现夜间模式,支持ListView.RecyclerView. Preview Usage xml android:background="?attr/zzbackground" app:backgroundAttr="zzbackground"//如果当前页面要立即刷新,这里传入属性名称 比如 R.attr.zzb

  • 三行Android代码实现白天夜间模式流畅切换

    Usage xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground //如果当前页面要立即刷新,这里传入属性名称 比如R.attr.zzbackground 传zzbackground即可 android:textColor= ?attr/zztextColor app:textColorAttr= zztextColor // 演示效果 Usage xml android:background="?

  • android基础教程之夜间模式实现示例

    复制代码 代码如下: package org.david.dayandnightdemo.cor; import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.graphics.Col

  • Android实现日夜间模式的深入理解

    在本篇文章中给出了三种实现日间/夜间模式切换的方案,三种方案综合起来可能导致文章的篇幅过长,请耐心阅读. 1.使用 setTheme 的方法让 Activity 重新设置主题: 2.设置 Android Support Library 中的 UiMode 来支持日间/夜间模式的切换: 3.通过资源 id 映射,回调自定义 ThemeChangeListener 接口来处理日间/夜间模式的切换. 一.使用 setTheme 方法 我们先来看看使用 setTheme 方法来实现日间/夜间模式切换的方

  • Android夜间模式最佳实践

    由于Android的设置中并没有夜间模式的选项,对于喜欢睡前玩手机的用户,只能简单的调节手机屏幕亮度来改善体验.目前越来越多的应用开始把夜间模式加到自家应用中,没准不久google也会把这项功能添加到Android系统中吧. 业内关于夜间模式的实现,有两种主流方案,各有其利弊,我较为推崇第三种方案: 1.通过切换theme来实现夜间模式. 2.通过资源id映射的方式来实现夜间模式. 3.通过修改uiMode来切换夜间模式. 值得一提的是,上面提到的几种方案,都是资源内嵌在Apk中的方案,像新浪微

  • Android 夜间模式的实现代码示例

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

  • Android实现老虎机小游戏代码示例

    用 Android studio软件写的一个老虎机小游戏 先上MainActivity.java 的代码.这里我用得定时器,本想用java线程,奈何安卓还不太会,应用会闪退. package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.View;

  • Android 情景模式的设置代码

    情景模式的设置大家应当相当熟悉了,但是在Android中如何通过自己的程序进行情景模式的设置呢,情景模式分为多种多种,即可以使用系统自带的,也可以使用自定义的,但是在开发某些程序时,可能需要在程序中更改情景模式,那么此就需要进行情景模式的设置.下面简单介绍一下情况模式的设置方式:首先获取当前的情景模式:代码 复制代码 代码如下: void getInitring(AudioManager audio)    {          //取得手机的初始音量,并初始化进度条        int vo

  • Android编程实现夜间模式的方法小结

    本文实例讲述了Android编程实现夜间模式的方法.分享给大家供大家参考,具体如下: 随着APP实现的功能越来越丰富, 看小说看视频上网等等, 现在不少人花在手机平板等移动终端上的时间越来越长了. 但手机和平板的屏幕并不像Kindle那类电纸书的水墨屏那么耐看, 由于自发光的屏幕特性, 我们长期盯着屏幕看容易眼睛酸痛疲倦, 因此各种护目模式, 夜间模式在移动APP上得到广泛应用, 这的确也是一个贴心的小功能. 所以这次我们探讨下几种实现方式, 一起学习总结下: 1. 利用屏幕亮度 当夜间使用手机

  • Android学习笔记--通过Application传递数据代码示例

    在整个Android程序中,有时需要保存某些全局的数据(如:用户信息),方便在程序的任何地方调用.在Activity之间数据传递中有一种比较使用的方式,就是全局对象,使用过J2EE的都应该知道JavaWeb的四个作用域,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于JavaWeb中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问. 在启动Application时,系统会创建

  • 使用javascript为网页增加夜间模式

    HTML+CSS: 复制代码 代码如下: <div class="cover"></div> 复制代码 代码如下: <style>.cover{    position:fixed;    top: 0px;    left: 0px;    outline:5000px solid rgba(0, 0, 0, 0.3);    z-index: 99999;}</style> 接着用JavaScript写个夜间模式plus: 复制代码

随机推荐