Android 夜间模式主题风格切换
程序员文章站
2022-05-14 23:53:15
...
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.运行结果
上一篇: 文本最末列数值乘一个数的批处理实现
下一篇: Vue组件化通讯的实例代码