Android实现夜间模式

如今非常多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


在Application中设置初始主题


动态切换


代码逻辑实现例如以下:

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);
    }
}
效果例如以下:



原文地址:https://www.cnblogs.com/lxjshuju/p/7400684.html