Android 夜间模式

最近项目中做一款阅读器,其中包含夜间模式和白天模式切换功能,最开始的想法是当点击了夜晚模式,手动更新背景颜色、文字颜色、以及icon。并且将设置保存在shared preference中。
这种方式工作了很大,代码臃肿,而且当界面越来越复杂时发现维护困难。查阅官方文档发现可以使用限定符轻松的实现夜晚/白天模式。

先看一下效果图,简单的夜间模式和白天模式切换


41e321aa-9c30-413c-8842-7ab9657058d7.gif

使用限定符实现白天/夜晚模式的切换

使用限定符处理白天/夜晚模式跟使用限定符处理横屏/竖屏类似。
回顾一下我们在处理横屏/竖屏的时候使用portland限定符:

  • port:设备处于纵向(垂直)
  • land:设备处于横向(水平)

MainActivity在横屏状态系统就会加载layout-land目录下的activity_main.xml ,并且activity_main.xml中使用到的图片资源会先去从drawable-land文件夹下寻找,其他资源例如colors、strings、styles等等会优先从values-land文件夹下寻找,如果没有则使用默认的。

类似的,白天和夜晚模式使用nightnotnight限定符:

  • night:夜间
  • notnight:白天

代码实现

1.创建夜晚模式对应的资源和资源文件( 默认是白天模式)
  • 新建drawable-night文件夹,放night模式下对应的icon
    注:notnight模式下的icon放在drawable文件下
image.png
drawable下的ic_read
drawable-night下的ic_read
  • 新建values-night文件夹,放night模式下对应的colors、strings
image.png

eg:
<color name="bg_color">#000000</color>
这是values-night/colors 背景颜色值

<color name="bg_color">#ffffff</color>
这是values/colors 背景颜色值

2.获取UIModeManager实例

final UiModeManager uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);

3.监听点击事件,手动设置模式
        findViewById(R.id.night_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
            }
        });

        findViewById(R.id.not_night_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
            }
        });

        findViewById(R.id.auto_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_AUTO);
            }
        });

UIModeManager 可设置三种模式

最后放上Activity和xml的全部代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/night_btn"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:background="@drawable/bg_button"
        android:text="夜间模式"
        app:layout_constraintBaseline_toBaselineOf="@+id/not_night_btn"
        app:layout_constraintEnd_toStartOf="@+id/not_night_btn" />

    <Button
        android:id="@+id/not_night_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="105dp"
        android:background="@drawable/bg_button"
        android:text="白天模式"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/auto_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:background="@drawable/bg_button"
        android:text="自动模式"
        app:layout_constraintBottom_toBottomOf="@+id/not_night_btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/not_night_btn" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="314dp"
        android:background="@color/bg_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|left"
            android:layout_marginLeft="20dp"
            android:drawableTop="@drawable/ic_read"
            android:text="阅读"
            android:textColor="@color/text_color" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableTop="@drawable/ic_read_categue"
            android:text="目录"
            android:textColor="@color/text_color" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|right"
            android:layout_marginRight="20dp"
            android:drawableTop="@drawable/ic_read"
            android:text="@string/mode"
            android:textColor="@color/text_color" />

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

activity_main.xml


package com.example.resourcesimple;

import android.app.UiModeManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "guo.MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate()--->");
        setContentView(R.layout.activity_main);

        final UiModeManager uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);

        findViewById(R.id.night_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
            }
        });

        findViewById(R.id.not_night_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
            }
        });

        findViewById(R.id.auto_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_AUTO);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart()--->");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume()--->");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause()--->");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop()--->");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy()--->");
    }
}

MainActivity.java


有问题的地方欢迎指出,欢迎评论 thinks.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,265评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,274评论 1 288
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,087评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,479评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,782评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,218评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,594评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,316评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,955评论 1 237
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,274评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,803评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,177评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,732评论 3 229
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,953评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,687评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,263评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,189评论 2 258

推荐阅读更多精彩内容