是否可以只为单个Activity设置夜间模式

Raj*_*tty 3 android android-layout

我正在活动的 onCreate 中设置夜间模式。因此,应用程序中的每个活动都会变成夜间模式,并且之前的所有活动都会重新创建。但我只需要该特定活动处于夜间模式,而所有其他活动都处于浅色模式。如何实现这一目标?下面的代码行我在活动的 oncreate 中使用以设置为夜间模式。

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

小智 5


尝试仅为单个活动设置模式


灯光模式

getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Run Code Online (Sandbox Code Playgroud)

深色模式

getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
Run Code Online (Sandbox Code Playgroud)

系统默认值

getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
Run Code Online (Sandbox Code Playgroud)