gpt4 book ai didi

java - 如何将主题应用到首选项屏幕

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:36 26 4
gpt4 key购买 nike

我正在 viewpager 中创建 PreferenceFragment 并收到错误

java.lang.IllegalStateException: Must specify preferenceTheme in theme

我提到了PreferenceFragmentCompat requires preferenceTheme to be set但没能解决。我添加了 styles.xml,如下所示。

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:preferenceStyle">@style/PreferenceThemeOverlay</item>
</style>

<style name="SettingsTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
</resources>

下面是 list 代码

  <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

以下是首选项屏幕的代码

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="screen">

<SwitchPreference
android:key="sc"
android:summary="Hi"
android:title="Hello" />

</PreferenceScreen>

你能给我建议吗,我第一次使用主题,并对它感到困惑。仍然遇到同样的错误。

最佳答案

manifest 中的 Activity 标记中尝试此操作:

<activity android:name=".MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

您加载首选项 fragment activity没有设置主题。

同时在您的Apptheme中进行设置

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

而不是:

<item name="android:preferenceStyle">@style/PreferenceThemeOverlay</item>

将此行添加到您的Apptheme

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

关于java - 如何将主题应用到首选项屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41662763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com