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



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


章节A:
Android实现白天/夜间模式主要控制器在于UiModeManager,UiModeManager是Android系统SDK本来就提供好的白天/夜间模式的实现控制系统。然而仅有UiModeManager还不够,同时需要写一套白天/夜间模式的资源,涉及到res/drawable或者values/color的设置。
现在给出一个例子加以说明实现Android白天/夜间模式的具体步骤和做法。

通常,在Android显示的布局中会有一些文本或者图,这些图基本就是一些icon图标,还有整体窗口的背景颜色。一般的,白天/夜间模式涉及到的显示状态的切换最基本改变影响就是这些文本、icon图标、整体的背景颜色等。

先给出本例代码运行结果,白天模式(默认缺省的模式):



点击夜间模式按钮后切换到的夜间显示模式:



章节B:
对章节A运行结果的说明。在APP整体切换白天/夜间模式过程中,最受影响和涉及的是一些背景颜色、图片icon、文本颜色等。在章节A中的图中,从上往下顺序排列了最具有典型意义的TextView、ImageView已经整体这个布局的背景颜色。初始状态是白天模式,可以看到按钮Button和TextView的字都是白色的,那个圆球ImageView是灰色的,标题栏是蓝色。当点击夜间模式按钮,进入夜间模式后,Button,TextView的字变成白色,ImageView圆球的颜色变成白色。整体的背景颜色换成黑色。
白天和夜间模式均可自由切换,而上层Java代码再后面详细展示后实际上很简单。


章节C:
实现章节A的白天/夜间模式。具体详情:
(1)先写上层Java代码,MainActivity.java:

package zhangphil.app;

import android.app.Activity;
import android.app.UiModeManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    //实现Android白天/夜间模式的关键类
    private UiModeManager mUiModeManager = null;

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

        mUiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);

        //点击白天模式按钮,切换到白天模式
        findViewById(R.id.day).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
            }
        });

        //点击夜间模式按钮,切换到夜间模式
        findViewById(R.id.night).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
            }
        });
    }
}


该MainActivity.java加载的activity_main.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="@color/background"
    android:orientation="vertical">

    <Button
        android:id="@+id/night"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="夜间模式"
        android:textColor="@color/text" />

    <Button
        android:id="@+id/day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="白天模式"
        android:textColor="@color/text" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:text="白天模式/夜间模式的切换
zhang phil @ csdn
http://blog.csdn.net/zhangphil"
        android:textColor="@color/text" />

</LinearLayout>

可以看到上层Java代码其实很简单,逻辑意图就是当用户点击切换白天/夜间模式后切换启动相应的逻辑。

接下去的才是关键。


(2)这一步才是实现白天/夜间模式的关键:资源文件的设置和编写。
当上层Java代码启动UiModeManager切换白天/夜间模式时候,Android系统会自动加载相应的资源。“相应的资源”是什么鬼?是res/values-night和res/drawable-night。
先说在代码中涉及到的颜色值。本例中涉及到的文本颜色值有Button和TextView,以及整体的背景颜色,这些颜色我在默认的res/values/colors.xml中已经定义,这也是默认的模式颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text">@android:color/black</color>
    <color name="background">@android:color/white</color>
    <color name="buttonBackground">@android:color/darker_gray</color>

    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

要做到夜间模式的颜色,我要做的就是在res目录下新建一个res/values-night目录,在values-night目录下,重新写一套colors.xml文件,在res/values-night/colors.xml文件中,将Android默认缺省的res/values/colors.xml中、自己开发的代码中涉及到属性定义再准备一套,而这套就是为夜间模式的颜色使用的,这个颜色值,将在Android系统调用setNightMode(UiModeManager.MODE_NIGHT_YES)后自动加载使用,res/values-night/colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text">@android:color/white</color>
    <color name="background">@android:color/black</color>
    <color name="buttonBackground">@android:color/darker_gray</color>

    <color name="colorPrimary">@android:color/darker_gray</color>
    <color name="colorPrimaryDark">@android:color/black</color>
</resources>


(3)由于受到白天/夜间模式影响的还有一些icon图片资源,仿照处理colors.xml的姿势,我再准备一套夜间模式的drawable,放到res/drawable-night中。在本例中,我放在res/drawable目录下的icon.png(白天、默认):


放在res/drawable-night目录下的icon.png(夜间):



(4)注意,res/values/colors.xml和res/values-night/colors.xml中定义的color必须同名, name相同,同理,res/drawable和res/drawable-night涉及到白天/夜间模式使用的icon图也必须同名,name相同。本例的目录结构:


代码运行结果就是章节A的结果。


附录:
1,《Android一键换肤功能:一种简单的实现》链接:http://blog.csdn.net/zhangphil/article/details/51455137

原文地址:https://www.cnblogs.com/hehehaha/p/6147246.html