android - 使用 AppCompatDelegate.setDefaultNightMode() 设置 DayNight 主题不适用于 FragmentActivity

标签 android android-fragmentactivity theme-daynight

我有点困惑。我在我的应用中使用 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>

最佳答案

MainActivity (which extends FragmentActivity)

FragmentActivity 不知道 AppCompat。 AppCompatDelegate 仅由 AppCompatActivity 使用,否则您必须wire it manually您的其他 Activity 。

您可以扩展 AppCompatActivity 而不是 FragmentActivity

关于android - 使用 AppCompatDelegate.setDefaultNightMode() 设置 DayNight 主题不适用于 FragmentActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61234417/

相关文章:

android - 将 PreferenceFragment 添加到 FragmentPagerAdapter

java - 切换日夜模式时如何为组件(工具栏、搜索栏、操作栏等)设置自定义颜色?

android - 带有 DayNight 主题的 BiometricPrompt

android - 由 : android. view.InflateException: Binary XML file line #11: Error inflating class fragment 引起

java - ListView、AbsListView 和 RecyclerView 有什么区别

android - 在 Linux 上运行 Android Studio 的一个问题

Android 蓝牙示例

android map 项目未清除

android - 使用 fragment 时应用程序崩溃