0

当 Android 设备设置为暗模式时。但是用户只想在这个应用程序上看到 Light 模式。有什么想法来处理这个吗?

这段代码对我不起作用

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

这些代码都不起作用

val config: Configuration = resources.getConfiguration()
        config.uiMode = Configuration.UI_MODE_NIGHT_NO
        resources.configuration.uiMode= Configuration.UI_MODE_NIGHT_NO
        applicationContext.createConfigurationContext(config)
        resources.updateConfiguration(config, getResources().getDisplayMetrics())
4

1 回答 1

0
I would like to see the method too, where you set once for all your activities. But as far I know you have to set in each activity before showing any views.

For reference check this:

http://www.anddev.org/applying_a_theme_to_your_application-t817.html

Edit (copied from that forum):

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Call setTheme before creation of any(!) View.
         setTheme(android.R.style.Theme_Dark);

        // ...
        setContentView(R.layout.main);
    }


Edit
If you call setTheme after super.onCreate(savedInstanceState); your activity recreated but if you call setTheme before super.onCreate(savedInstanceState); your theme will set and activity does not recreate anymore

  protected void onCreate(Bundle savedInstanceState) {
     setTheme(android.R.style.Theme_Dark);
     super.onCreate(savedInstanceState);


    // ...
    setContentView(R.layout.main);
}
于 2020-12-22T10:51:19.647 回答