Android 首选项屏幕主题不起作用
Android Preference Screen theme not working
我正在我的应用程序中设置一个带有导航架构组件的 Androidx 首选项屏幕。出于某种原因,Android 首选项屏幕看起来像这样
当我从 Android Studio 主题切换器切换主题时,它看起来非常好。
这是我的偏好 XML 布局代码
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:key="application_settings"
app:singleLineTitle="true">
<PreferenceCategory
android:layout_width="match_parent"
android:layout_height="match_parent"
app:iconSpaceReserved="false"
app:singleLineTitle="true"
app:title="@string/display_language_header">
<ListPreference
android:layout_width="match_parent"
android:layout_height="match_parent"
app:entries="@array/Languages"
app:entryValues="@array/Languages_Values"
app:icon="@drawable/ic_language"
app:key="application_language"
app:singleLineTitle="true"
app:summary="@string/default_application_language"
app:title="@string/application_settings_language" />
</PreferenceCategory>
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:iconSpaceReserved="false"
app:singleLineTitle="true"
app:title="@string/normal_settings_title">
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:icon="@drawable/ic_sound"
app:key="sound_setting"
app:singleLineTitle="true"
app:summary="@string/sound_settings_summary"
app:title="@string/sound_settings_title" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/vibration_settings_summary"
android:title="@string/vibration_settings_title"
app:icon="@drawable/ic_vibrate"
app:key="vibration_setting"
app:singleLineTitle="true" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/auto_copy_to_clip_board_settings_summary"
android:title="@string/auto_copy_to_clipboard_settings_title"
app:icon="@drawable/ic_copy"
app:key="copy_to_clip_board_setting"
app:singleLineTitle="true" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/auto_web_search_settings_summary"
android:title="@string/auto_web_search_settings_title"
app:icon="@drawable/ic_search"
app:key="auto_web_search_setting"
app:singleLineTitle="true" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/save_history_settings_summary"
android:title="@string/save_history_settings_title"
app:icon="@drawable/ic_save"
app:key="save_history_setting"
app:singleLineTitle="true" />
</PreferenceCategory>
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:iconSpaceReserved="false"
app:singleLineTitle="true"
app:title="@string/application_settings_title">
<Preference
android:id="@+id/privacy_policy"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="privacy_policy"
android:summary="@string/privacy_policy_settings_summary"
android:title="@string/privacy_policy_settings_title"
app:singleLineTitle="true" />
<Preference
android:id="@+id/share_with_friends"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="share_with_friends"
android:summary="@string/share_with_friends_summary"
android:title="@string/share_with_friends_title"
app:singleLineTitle="true" />
<Preference
android:id="@+id/rate_us"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="rate_us"
android:summary="@string/rate_us_summary"
android:title="@string/rate_us_summary"
app:singleLineTitle="true" />
<Preference
android:id="@+id/delete_history"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="delete_history"
android:summary="@string/delete_history_summary"
android:title="@string/delete_history_title"
app:singleLineTitle="true" />
</PreferenceCategory>
我的申请风格
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorSecondary">@color/colorPrimary</item>
<item name="fontFamily">@font/regular</item>
<item name="android:orientation">vertical</item>
</style>
我尝试为首选项屏幕提供硬编码主题,例如 android:theme="@style/AppTheme",但它也不起作用。我正在使用导航控制器通过底部应用栏调用 Preference Fragment。请帮助我了解我做错了什么?
我找到了这个答案的修复方法。我在 Android 清单中有 android:orientation
,这导致首选项屏幕采用垂直方向而不是水平方向。现在它看起来像一个正常的首选项屏幕。
我正在我的应用程序中设置一个带有导航架构组件的 Androidx 首选项屏幕。出于某种原因,Android 首选项屏幕看起来像这样
当我从 Android Studio 主题切换器切换主题时,它看起来非常好。
这是我的偏好 XML 布局代码
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:key="application_settings"
app:singleLineTitle="true">
<PreferenceCategory
android:layout_width="match_parent"
android:layout_height="match_parent"
app:iconSpaceReserved="false"
app:singleLineTitle="true"
app:title="@string/display_language_header">
<ListPreference
android:layout_width="match_parent"
android:layout_height="match_parent"
app:entries="@array/Languages"
app:entryValues="@array/Languages_Values"
app:icon="@drawable/ic_language"
app:key="application_language"
app:singleLineTitle="true"
app:summary="@string/default_application_language"
app:title="@string/application_settings_language" />
</PreferenceCategory>
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:iconSpaceReserved="false"
app:singleLineTitle="true"
app:title="@string/normal_settings_title">
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:icon="@drawable/ic_sound"
app:key="sound_setting"
app:singleLineTitle="true"
app:summary="@string/sound_settings_summary"
app:title="@string/sound_settings_title" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/vibration_settings_summary"
android:title="@string/vibration_settings_title"
app:icon="@drawable/ic_vibrate"
app:key="vibration_setting"
app:singleLineTitle="true" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/auto_copy_to_clip_board_settings_summary"
android:title="@string/auto_copy_to_clipboard_settings_title"
app:icon="@drawable/ic_copy"
app:key="copy_to_clip_board_setting"
app:singleLineTitle="true" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/auto_web_search_settings_summary"
android:title="@string/auto_web_search_settings_title"
app:icon="@drawable/ic_search"
app:key="auto_web_search_setting"
app:singleLineTitle="true" />
<SwitchPreferenceCompat
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:summary="@string/save_history_settings_summary"
android:title="@string/save_history_settings_title"
app:icon="@drawable/ic_save"
app:key="save_history_setting"
app:singleLineTitle="true" />
</PreferenceCategory>
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:iconSpaceReserved="false"
app:singleLineTitle="true"
app:title="@string/application_settings_title">
<Preference
android:id="@+id/privacy_policy"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="privacy_policy"
android:summary="@string/privacy_policy_settings_summary"
android:title="@string/privacy_policy_settings_title"
app:singleLineTitle="true" />
<Preference
android:id="@+id/share_with_friends"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="share_with_friends"
android:summary="@string/share_with_friends_summary"
android:title="@string/share_with_friends_title"
app:singleLineTitle="true" />
<Preference
android:id="@+id/rate_us"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="rate_us"
android:summary="@string/rate_us_summary"
android:title="@string/rate_us_summary"
app:singleLineTitle="true" />
<Preference
android:id="@+id/delete_history"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:key="delete_history"
android:summary="@string/delete_history_summary"
android:title="@string/delete_history_title"
app:singleLineTitle="true" />
</PreferenceCategory>
我的申请风格
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorSecondary">@color/colorPrimary</item>
<item name="fontFamily">@font/regular</item>
<item name="android:orientation">vertical</item>
</style>
我尝试为首选项屏幕提供硬编码主题,例如 android:theme="@style/AppTheme",但它也不起作用。我正在使用导航控制器通过底部应用栏调用 Preference Fragment。请帮助我了解我做错了什么?
我找到了这个答案的修复方法。我在 Android 清单中有 android:orientation
,这导致首选项屏幕采用垂直方向而不是水平方向。现在它看起来像一个正常的首选项屏幕。