Android 夜间模式主题风格切换

1. 跟进源码

1.1 String.xml

Settings 模块

    <string name="night_display_title" msgid="2626451512200357686">"夜间模式"</string>
    <string name="night_display_text" msgid="1837277457033025056">"夜间模式会将您的屏幕色调调节为琥珀色,让您在光线昏暗的环境下可以更舒适地查看屏幕或阅读文字,并可能有助于您入睡。"</string>
    <string name="night_display_category_schedule" msgid="2044072617637348966">"排定时间"</string>
    <string name="night_display_category_status" msgid="1952928783124400330">"状态"</string>
    <string name="night_display_auto_mode_title" msgid="8046314360381608455">"自动开启"</string>
    <string name="night_display_auto_mode_never" msgid="6723636142053240947">"永不"</string>
    <string name="night_display_auto_mode_custom" msgid="6012300346981608242">"自定义时间安排"</string>
    <string name="night_display_auto_mode_twilight" msgid="2123345097508167094">"日落到日出"</string>
    <string name="night_display_start_time_title" msgid="8918016772613689584">"开始时间"</string>
    <string name="night_display_end_time_title" msgid="8286061578083519350">"结束时间"</string>
    <string name="night_display_summary_off" msgid="7009821232380000786">"关闭。<xliff:g id="ID_1">%1$s</xliff:g>"</string>
    <string name="night_display_summary_off_auto_mode_never" msgid="2305501561697289620">"一律不自动开启。"</string>
    <string name="night_display_summary_off_auto_mode_custom" msgid="1548073080728058384">"将在<xliff:g id="ID_1">%1$s</xliff:g>自动开启。"</string>
    <string name="night_display_summary_off_auto_mode_twilight" msgid="2737328390752018845">"将在日落时自动开启。"</string>
    <string name="night_display_summary_on" msgid="6626224414971753417">"开启。<xliff:g id="ID_1">%1$s</xliff:g>"</string>
    <string name="night_display_summary_on_auto_mode_never" msgid="5217396859715040056">"一律不自动关闭。"</string>
    <string name="night_display_summary_on_auto_mode_custom" msgid="887702685252504739">"将在<xliff:g id="ID_1">%1$s</xliff:g>自动关闭。"</string>
    <string name="night_display_summary_on_auto_mode_twilight" msgid="8574989770628425398">"将在日出时自动关闭。"</string>

1.2 布局文件

Settings 模块 display_settings.xml,设置入口

        <com.android.settings.display.NightDisplayPreference
                android:key="night_display"
                android:title="@string/night_display_title"
                android:fragment="com.android.settings.display.NightDisplaySettings"
                android:widgetLayout="@null"
                settings:keywords="@string/keywords_display_night_display" />

        <!-- Hide night mode for now
        <ListPreference
            android:key="night_mode"
            android:title="@string/night_mode_title"
            settings:keywords="@string/keywords_display_night_mode"
            android:summary="@string/night_mode_summary"
            android:entries="@array/night_mode_entries"
            android:entryValues="@array/night_mode_values" /> -->

1.3 逻辑控制

UiModeManager.setNightMode(value);进行夜间模式和白天模式的切换

package com.android.settings;

public class DisplaySettings extends SettingsPreferenceFragment implements
        Preference.OnPreferenceChangeListener, Indexable {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Activity activity = getActivity();
        final ContentResolver resolver = activity.getContentResolver();

        addPreferencesFromResource(R.xml.display_settings);


        mNightModePreference = (ListPreference) findPreference(KEY_NIGHT_MODE);
        if (mNightModePreference != null) {
            final UiModeManager uiManager = (UiModeManager) getSystemService(
                    Context.UI_MODE_SERVICE);
            final int currentNightMode = uiManager.getNightMode();
            mNightModePreference.setValue(String.valueOf(currentNightMode));
            mNightModePreference.setOnPreferenceChangeListener(this);
        }
        ...
        import android.app.UiModeManager;

        if (preference == mNightModePreference) {
            try {
                final int value = Integer.parseInt((String) objValue);
                final UiModeManager uiManager = (UiModeManager) getSystemService(
                        Context.UI_MODE_SERVICE);
                uiManager.setNightMode(value);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist night mode setting", e);
            }
        }

2.Demo

1.原理

调用UiModeManager夜间模式接口 + value-night资源文件

2.实现逻辑
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);
    }


    public void night(View view) {
        mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
    }

    public void day(View view) {
        mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
    }
3.布局文件
    <Button
        android:onClick="night"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="夜间模式" />

    <Button
        android:onClick="day"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="白天模式" />
4.夜间模式和白天模式的资源文件

values-night

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3B5125</color>
    <color name="colorPrimaryDark">#30E333</color>
    <color name="colorAccent">#FF4081</color>
    <color name="mybackground">#000000</color>
</resources>
5.运行结果

DAY

NIGHT

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值