【问题标题】:Setting DayNight theme using AppCompatDelegate.setDefaultNightMode() not working for FragmentActivity使用 AppCompatDelegate.setDefaultNightMode() 设置 DayNight 主题不适用于 FragmentActivity
【发布时间】:2020-04-16 15:07:43
【问题描述】:

我有点困惑。我在我的应用程序中使用了 DayNight 主题(使用 AppCompatDelegate.setDefaultNightMode()),但无法在我的 MainActivity 中使用它。 MainActivity(扩展 FragmentActivity)看起来从未设置为深色主题 - 它始终保持浅色主题

我尝试直接在我的 MainActivity 中设置主题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    super.onCreate(savedInstanceState);
    // create main activity.
}

但这不起作用。

我已经使用 ?attr/colorReference 正确设置了布局文件中的所有颜色。有谁知道这里出了什么问题?

编辑: 我的styles.xml如下:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!--Default typeface and colors:-->
    <item name="android:typeface">monospace</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorAccentDarker">@color/colorAccentDarker</item>
    <item name="colorAccentDarker_80percent">@color/colorAccentDarker_80percent</item>

    <!--Show people's own wallpaper background-->
    <item name="android:windowShowWallpaper">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

【问题讨论】:

  • 你能告诉我们你的style.xml吗?

标签: android android-fragmentactivity theme-daynight


【解决方案1】:

MainActivity(扩展FragmentActivity

FragmentActivity 不知道 AppCompat。 AppCompatDelegate 仅供AppCompatActivity 使用,否则您必须wire it manually 才能进行其他活动。

您可以扩展AppCompatActivity 而不是FragmentActivity

【讨论】:

  • AppCompatActivity 扩展FragmentActivity
  • 这对我有用,但引入了这些副作用:弹出一个丑陋的标题,按钮的透明背景不再透明
  • @riezebosch 1. 我怀疑你在主题中设置了 android:background,不要那样做,它是一个小部件/样式属性。您可能想谷歌“主题与样式 android”并阅读差异。 2. AppCompat 按钮样式是“buttonStyle”,而不是普通 Android 的“android:buttonStyle”。许多其他小部件也效仿。相应地更新您的主题,它应该会再次工作。
  • 如果您有任何想法,请帮助解决此设备特定问题:stackoverflow.com/questions/71333304/…
猜你喜欢
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2015-10-27
相关资源
最近更新 更多