当手机设置为深色模式时。
但用户只想在此应用程序上选择主题(白天/夜晚)。
有什么想法可以在运行时以编程方式更改应用程序主题吗?
您可以使用它来更改它
//Turn on night mode
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
//Turn off night mode
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
我使用这样的开关将其应用到应用程序中
Switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
});
添加 uiMode 配置更改:
在 AndroidManifest 中
<activity
android:name=".MyActivity"
android:configChanges="uiMode" />
使用此代码强制光/日模式还需要重新创建活动
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
recreate()
使用此代码强制黑暗/夜间模式还需要重新创建活动
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
recreate()
说实话,这个 Kotlin 代码对我来说工作得很好(也应该在系统默认情况下添加主题)
val currentNightMode =
resources.getConfiguration().uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
Configuration.UI_MODE_NIGHT_NO -> {
if (getAppSetting(
getString(R.string.key_setting_theme),
Context.MODE_PRIVATE
) == Configuration.UI_MODE_NIGHT_YES
)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
recreate()
} // Night mode is not active, we're using the light theme
Configuration.UI_MODE_NIGHT_YES -> {
if (getAppSetting(
getString(R.string.key_setting_theme),
Context.MODE_PRIVATE
) == Configuration.UI_MODE_NIGHT_NO
)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
recreate()
} // Night mode is active, we're using dark theme
}
还有更多选择
MODE_NIGHT_NO. Always use the day (light) theme.
MODE_NIGHT_YES. Always use the night (dark) theme.
MODE_NIGHT_FOLLOW_SYSTEM (default). This setting follows the system’s setting, which on Android Q and above is a system setting (more on this below).
MODE_NIGHT_AUTO_BATTERY. Changes to dark when the device has its ‘Battery Saver’ feature enabled, light otherwise.
MODE_NIGHT_AUTO_TIME & MODE_NIGHT_AUTO. Changes between day/night based on the time of day.
了解更多信息 https://developer.android.com/guide/topics/ui/look-and-feel/darktheme
通过新的更新,我现在认为这种配置应用程序夜间模式的新方法更好。
实施并启动属性。
MaterialSwitch switchMaterial;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
boolean darkMode;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taller_screen);
switchMaterial = findViewById(R.id.switchMaterial);
sharedPreferences = getSharedPreferences("MODE",
Context.MODE_PRIVATE);
darkMode = sharedPreferences.getBoolean("night", false);
if(darkMode){
switchMaterial.setChecked(true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
switchMaterial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (darkMode){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
editor = sharedPreferences.edit();
editor.putBoolean("night", false);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
editor = sharedPreferences.edit();
editor.putBoolean("night", true);
}
editor.apply();
}
});
}
您只需要此代码即可设置夜间模式。祝你好运!