【问题标题】:Android DayNight Theme with configChanges带有 configChanges 的 Android DayNight 主题
【发布时间】:2021-12-07 17:22:27
【问题描述】:

我正在创建一个应用,其中使用 Android 支持库的主题 DayNight。

这是themes.xml中的代码

<style name="ActivityTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

我在清单中使用 configChanges 来管理轮换。

android:configChanges="keyboardHidden|orientation|screenSize"

在我的 AppcompatActivity 的 onCreate 中,我添加了:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

问题是当我使用 setDefaultNightMode 时,android:configChanges 停止工作,并在每次轮换中重新创建活动。

欢迎任何形式的帮助

【问题讨论】:

    标签: android rotation orientation


    【解决方案1】:

    似乎是一个错误,将根据此issue 在 AppCompat v.1.1.0 中修复。

    在修复之前,我会将uiMode 标志添加到android:configChanges,就像提到的here

    【讨论】:

      【解决方案2】:

      您可能需要在 ma​​nifest 中添加“uiMode”参数:

      android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
      

      【讨论】:

        【解决方案3】:

        issue 已使用 AppCompat v1.1.0-alpha05 修复。

        请看这里:https://developer.android.com/preview/features/darktheme#changing_themes_in-app

        注意:从AppCompat v1.1.0-alpha05 开始,setDefaultNightMode() 不会自动重新创建任何已启动的活动。

        将您的 appcompat 依赖项更新为 latest 版本,当前为 1.1.0-rc01。

        implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
        

        如果您添加了 uiMode 标志,请不要忘记从清单中的 android:configChanges 中删除它。

        【讨论】:

        • 我们正在使用最新的依赖项 'androidx.appcompat:appcompat:1.1.0',但在 onConfig 更改中 ui 主题仍未更新
        【解决方案4】:

        从 AppCompat v1.1.0 开始,setDefaultNightMode() 会自动重新创建所有已启动的活动。

        https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#changing_themes_in-app

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-04
          • 1970-01-01
          • 1970-01-01
          • 2020-01-21
          相关资源
          最近更新 更多