android - 在 PreferenceFragmentCompat 中为 PreferenceCategory 设置标题样式

标签 android android-layout android-fragments android-preferences android-styles

我想为我的偏好 fragment 屏幕V14设置标题样式。
这就是我想要的:
enter image description here

我已经关注了
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>

我得到的是这个:
enter image description here

最佳答案

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/

相关文章:

Android:Android 4.1 模拟器从 DatePicker 对话框调用 onDateSet 两次

Android View 动画

android - 使用主题定义的设置还是来自设计师规范?

java - 将 fragment 布局纵向更改为横向

javascript - 固定位置在移动浏览器中不起作用

android - 计步器(计步器)

java - ImageAdapter 不能应用于 Fragment 类

android - 相同的 fragment 、edittext 和 requestfocus 问题

java - 使用 Edittext 更改 Textview 的文本?

android - 如何设置自定义切换按钮android的文本