应用程序中用于夜间模式的 Android DayNight 主题
在本教程中,我们将在我们的应用程序中讨论和使用 Android DayNight 主题。如果您有带阅读材料的应用程序,那么夜间模式有助于保护眼睛。
安卓日夜主题
Android 发布了一个新主题:Theme.AppCompat.DayNight,支持库 23.2.0。多亏了这个主题,我们现在可以在应用程序的明暗模式之间切换。我们可以手动执行此操作,也可以让 Android 从您的手机中隐式检测一天中的时间。该主题通过用较暗的白色背景替换华丽的背景,增强了您的应用程序在夜间的可读性和可用性。许多阅读器应用程序已经在他们的应用程序中部署了这个主题。让我们通过创建一个包含空活动的新 Android Studio 项目来开始我们的实施。
将主题添加到我们的 styles.xml
让我们用 DayNight 主题替换我们应用程序中的当前主题。
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
要在我们的应用程序中设置 DayNight 主题,我们使用方法:AppCompatDelegate.setDefaultNightMode() 以下是上述方法中允许的参数。
MODE_NIGHT_YES– 手动启用夜间模式。MODE_NIGHT_NO– 手动禁用夜间模式。MODE_NIGHT_FOLLOW_SYSTEM– 使用系统设置来确定一天中的时间并相应地切换 NightMode。这是默认参数。MODE_NIGHT_AUTO– 这会尝试从设备位置 API 自动检测时间。如果未授予位置服务的运行时权限,则使用系统时间。
在 onCreate() 方法中添加以下代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); //For night mode theme
//AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); //For day mode theme
setContentView(R.layout.activity_main);
}
应始终在调用 setContentView 方法之前设置主题。
什么是 AppCompatDelegate?

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
要检索当前的夜间模式类型,我们使用方法 AppCompatDelegate.getDefaultNightMode(),它分别为前面讨论的每种类型返回一个整数。有了基本的想法后,让我们制作一个应用程序:
- 在白天/夜间模式下自定义资源和样式。
- 从 UI 切换 DayNight 主题
- 查看各种 UI 小部件在夜间模式下的外观。
Android 夜间模式项目结构

Android DayNight 主题示例代码
下面给出了 activity_main.xml 类文件的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="@android:dimen/app_icon_size"
android:text="Welcome to this tutorial."
android:textColor="@color/daynight_textColor"
android:textSize="18sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:src="@drawable/placeholder" />
<TextView
android:id="@+id/txtNightMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/switchCompat"
android:layout_centerHorizontal="true"
android:paddingRight="8dp"
android:text="Night Mode"
android:textColor="@color/daynight_textColor" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/switchCompat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@android:dimen/app_icon_size"
android:layout_toRightOf="@+id/txtNightMode"
android:checked="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView"
android:layout_alignLeft="@+id/txtNightMode"
android:layout_alignStart="@+id/txtNightMode"
android:text="CLICK ME"
android:textColor="@color/daynight_textColor" />
</RelativeLayout>
- 我们在 ImageView 上设置了自定义文本颜色和可绘制对象。
- 要为日夜主题设置不同的颜色和可绘制对象,我们需要为资源创建单独的文件夹。
- Day 主题资源位于默认目录中。
- 夜间主题资源位于名称后缀为 -night 的文件夹中。
- 因此,我们在项目中创建了
values-night和drawable-night文件夹。 - 您希望在 DayNight 主题中切换的资源的两个目录中的可绘制文件名、颜色、样式名称必须相同。
- 如果以上内容仅在一个目录中定义,则日夜主题中将使用相同的内容。
下面给出了 values 和 values-night 文件夹中的 styles.xml 的代码。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@android:color/white</item>
</style>
<style name="MyDialog" parent="Theme.AppCompat.Light.Dialog.Alert"/>
<style name="MySwitch">
<item name="colorControlActivated">@color/switchColor</item>
</style>
</resources>
<resources>
<!-- Base application theme. values-night.xml -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/orange</item>
<item name="colorPrimaryDark">@color/orangeDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@android:color/white</item>
</style>
<style name="MyDialog" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>
<style name="MySwitch">
<item name="colorControlActivated">@color/switchColor</item>
</style>
</resources>

package com.journaldev.daynightmode;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (InitApplication.getInstance().isNightModeEnabled()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
setContentView(R.layout.activity_main);
SwitchCompat switchCompat = findViewById(R.id.switchCompat);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this, R.style.MyDialog)
.setTitle("Title")
.setMessage("Message")
.show();
}
});
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES)
switchCompat.setChecked(true);
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
InitApplication.getInstance().setIsNightModeEnabled(true);
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
} else {
InitApplication.getInstance().setIsNightModeEnabled(false);
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
}
}
});
}
}
在上面的代码中,我们使用 Switch 在我们的应用程序中的白天和夜间模式主题之间切换。我们将当前模式保存在应用程序类的单例模式中。这样,可以在整个应用程序中使用相同的 Application 类实例。 InitApplication.java 类的代码如下所示。
package com.journaldev.daynightmode;
import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class InitApplication extends Application {
public static final String NIGHT_MODE = "NIGHT_MODE";
private boolean isNightModeEnabled = false;
private static InitApplication singleton = null;
public static InitApplication getInstance() {
if(singleton == null)
{
singleton = new InitApplication();
}
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
singleton = this;
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
this.isNightModeEnabled = mPrefs.getBoolean(NIGHT_MODE, false);
}
public boolean isNightModeEnabled() {
return isNightModeEnabled;
}
public void setIsNightModeEnabled(boolean isNightModeEnabled) {
this.isNightModeEnabled = isNightModeEnabled;
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(NIGHT_MODE, isNightModeEnabled);
editor.apply();
}
}
在这里,我们从共享首选项中更新和检索夜间模式类型。
安卓夜间模式支持应用输出

下载 Android DayNight 主题示例项目