现在很多App都有夜间模式,特别是阅读类的App,夜间模式现在已经是阅读类App的标配了,事实上,日间模式与夜间模式就是给App定义并应用两套不同颜色的主题,用户可以自动或者手动的开启,今天用Android自带的support包来实现夜间模式。由于Support Library在23.2.0的版本中才添加了Theme.AppCompat.DayNight主题,所以依赖的版本必须是高于23.2.0的,并且,这个特性支持的最低SDK版本为14,所以,需要兼容Android 4.0的设备,是不能使用这个特性的,在API Level 14以下的设备会默认使用亮色主题。不过现在4.0以下的设备应该比较少了吧,毕竟微信的minSdkVersion都设置为14了。
添加依赖
准备资源
让应用继承DayNight主题
<resources>新建夜间模式资源文件夹:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
在res目录下新建values-night
文件夹,然后在此目录下新建colors.xml
文件在夜间模式下的应用的资源。当然也可以根据需要新建drawable-night
,layout-night
等后缀为-night
的夜间资源文件夹。如下:
内容如下:
values/colors.xml
<?xml version="1.0" encoding="utf-8"?><!-- day values colors.xml --><resources> <color name="colorPrimary">#009688</color> <color name="colorPrimaryDark">#00796B</color> <color name="colorAccent">#009688</color> <color name="textColorPrimary">#616161</color> <color name="viewBackground">@android:color/white</color> <color name="colorDayNightChange">@android:color/holo_orange_dark</color></resources>values/strings.xml
<resources> <string name="app_name">DayNight</string> <string name="day_night_label">日间模式</string></resources>values-night/colors.xml
<?xml version="1.0" encoding="utf-8"?><!-- night values colors.xml --><resources> <color name="colorPrimary">#35464e</color> <color name="colorPrimaryDark">#212a2f</color> <color name="colorAccent">#212a2f</color> <color name="textColorPrimary">#616161</color> <color name="viewBackground">#212a2f</color> <color name="colorDayNightChange">@android:color/holo_blue_dark</color></resources>values-night/strings.xml
<resources> <string name="app_name">DayNight</string> <string name="day_night_label">夜间模式</string></resources>
使Activity继承自AppCompatActivity
动态切换
代码逻辑实现如下:
acitivity_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:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/day_night_label" android:textSize="20sp" android:textColor="@color/colorDayNightChange" /> <Button android:id="@+id/day_night_change" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="5dp" android:text="日夜间模式切换" android:textSize="20sp" android:textColor="@color/colorDayNightChange"/></LinearLayout>
MainActivity.java
package com.jackie.daynight;import android.content.res.Configuration;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.app.AppCompatDelegate;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button mDayNightChange; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDayNightChange = (Button) findViewById(R.id.day_night_change); mDayNightChange.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; if (mode == Configuration.UI_MODE_NIGHT_YES) { getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO); } else if (mode == Configuration.UI_MODE_NIGHT_NO) { getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES); } recreate(); } }); }}
MyApplication.java
package com.jackie.daynight;import android.app.Application;import android.support.v7.app.AppCompatDelegate;/** * Created by Jackie on 2017/3/6. * Application */public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); /** * 默认设置一直使用夜间模式 * * 这里AppCompatDelegate.setDefaultNightMode()方法可以接受的参数值有4个: * MODE_NIGHT_NO. Always use the day (light) theme(一直应用日间(light)主题). * MODE_NIGHT_YES. Always use the night (dark) theme(一直使用夜间(dark)主题). * MODE_NIGHT_AUTO. Changes between day/night based on the time of day(根据当前时间在day/night主题间切换). * MODE_NIGHT_FOLLOW_SYSTEM(默认选项). This setting follows the system’s setting, which is essentially MODE_NIGHT_NO(跟随系统,通常为MODE_NIGHT_NO). */ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); }}效果如下: