【问题标题】:How to apply light(day) theme at run time (programatically) on Android如何在运行时(以编程方式)在 Android 上应用 light(day) 主题
【发布时间】:2020-12-22 09:00:16
【问题描述】:

当 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())

【问题讨论】:

  • 你可以看看这个answer
  • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) 对我不起作用

标签: android material-design android-darkmode


【解决方案1】:
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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多