我想为我的偏好 fragment 屏幕V14设置标题样式。
这就是我想要的:
我已经关注了
Custom PreferenceCategory Headings
我确实设法获得了相同的屏幕,但使用了 PreferenceFragment!!
我该如何为 PreferenceFragmentCompat V14 做这件事??
这是我的代码
Style.xml
<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:listSeparatorTextViewStyle">@style/PreferenceStyle</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>
<style name="PreferenceStyle" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/colorDialogPop</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">@dimen/text_medium</item>
<item name="android:padding">@dimen/padding_large</item>
<item name="android:layout_marginLeft">@dimen/margin_medium</item>
<item name="android:background">@color/colorAccent</item>
</style>
preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/heading_general"
android:layout="@layout/settings_text">
<SwitchPreference
android:padding="@dimen/padding_medium"
android:title="@string/enable_push" />
<SwitchPreference
android:padding="@dimen/padding_medium"
android:title="@string/send_email" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/heading_account"
android:layout="@layout/settings_text">
<EditTextPreference
android:padding="@dimen/padding_medium"
android:title="@string/email" />
<EditTextPreference
android:padding="@dimen/padding_medium"
android:title="@string/name" />
</PreferenceCategory>
</PreferenceScreen>
setting_text.xml 用于标题文本的布局
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/PreferenceStyle"
>
</TextView>
最佳答案
Material 样式 PreferenceCategory
不使用 android:listSeparatorTextViewStyle
。您可以用不同的布局替换所有 PreferenceCategory
和您想要的样式,而不是试图弄乱系统样式。
在 res/layout/custom_preference_category.xml
中:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/title"
style="@style/CustomPreferenceCategoryText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_medium" />
在 res/values/styles.xml
中:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/PreferenceStyle</item>
</style>
<style name="PreferenceStyle" parent="@style/PreferenceThemeOverlay.v14.Material">
<item name="preferenceCategoryStyle">@style/CustomPreferenceCategory</item>
</style>
<style name="CustomPreferenceCategory" parent="@style/Preference.Category">
<item name="android:layout">@layout/custom_preference_category</item>
</style>
<style name="CustomPreferenceCategoryText" parent="@android:style/Widget.TextView">
<!-- Style your PreferenceCategory here. -->
<item name="android:textColor">@color/colorDialogPop</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">@dimen/text_medium</item>
<item name="android:padding">@dimen/padding_large</item>
<item name="android:background">@color/colorAccent</item>
</style>
关于android - 在 PreferenceFragmentCompat 中为 PreferenceCategory 设置标题样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38279034/