如何更改 PreferenceScreen 中 PreferenceCategory 分隔符的颜色
How to change the color of the PreferenceCategory divider in PreferenceScreen
我有一个 android.support.v4.preference.PreferenceFragment,它使用以下 PreferenceScreen:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Cat1"
android:key="pref_cat1">
<ListPreference
android:key="pref_list1"
android:title="@string/pref_list1"
android:dialogTitle="@string/pref_list1"
android:entries="@array/pref_list1_entries"
android:entryValues="@array/pref_list1_entries"
android:defaultValue="@string/pref_list1_default"/>
<EditTextPreference
android:key="pref_text2"
android:title="@string/pref_text2"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="Cat2"
android:key="pref_cat2">
<EditTextPreference
android:key="pref_text3"
android:title="@string/pref_text3"
/>
</PreferenceCategory>
显示 PreferenceFragment 时,一些分隔符会显示在首选项之间,但也会显示在每个 PreferenceCategory 的名称下方。
虽然我可以通过访问 PreferenceFragment 的 ListView 轻松修改首选项之间分隔线的颜色,但这对 PreferenceCategory 分隔线没有影响。
如何更改此类分隔线的颜色?
您有两个选择:
- 在您的应用主题中定义
listSeparatorTextViewStyle
请注意,依赖此主题属性的任何其他内容也将更改为使用您定义的样式。如果您不介意,它看起来像这样:
<style name="AppTheme" parent="android:...">
...
<item name="android:listSeparatorTextViewStyle">@style/ListSeparatorText</item>
</style>
<style name="ListSeparatorText" parent="android:Widget.TextView"><!--parent is optional -->
<item name="android:background">...</item>
...
</style>
- 为您的 PreferenceCategories 定义自定义布局
PreferenceCategory
的默认布局只是一个 TextView。您可以根据需要使布局尽可能简单或复杂,但某处应该是带有 android:id="@android:id/title"
的 TextView,以便自动绑定标题。
有了布局后,请在您的首选项中使用 android:layout
属性 xml:
<PreferenceCategory
android:title="Cat2"
android:key="pref_cat2"
android:layout="@layout/my_pref_category">
...
</PreferenceCategory>
或者,您可以在应用程序的主题中定义 preferenceCategoryStyle
,在这种情况下,您根本不需要根据自己的喜好使用 android:layout
xml。
<style name="AppTheme" parent="android:...">
...
<item name="android:preferenceCategoryStyle">@style/PreferenceCategoryStyle</item>
</style>
<style name="PreferenceCategoryStyle" parent="android:Preference.Category">
<item name="android:layout">@layout/my_pref_category</item>
...
</style>
我的 Prefernces
文件中也有同样的问题。我通过以下步骤解决了它:
1:定义文件名preferences_category.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+android:id/title"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<View style="@style/Divider" />
</LinearLayout>
2: 在样式 xml 文件中定义 style
:
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">@android:color/white</item>
</style>
3: 将 preferences_category.xml
文件作为 android:layout
属性添加到 preferences
xml 文件中::
<PreferenceCategory
android:title="My title"
android:layout="@layout/preferences_category">
现在您还可以在代码中为分隔线设置自定义可绘制对象。我不确定旧版本的支持库,但至少现在使用 AndroidX 它对我有用。
在您的PreferenceFragmentCompat
中您可以添加:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setDivider(getResources().getDrawable(R.drawable.your_divider_drawable, requireActivity().getTheme()));
}
您也可以调用 setDivider(null)
来删除分隔符。
我有一个 android.support.v4.preference.PreferenceFragment,它使用以下 PreferenceScreen:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Cat1"
android:key="pref_cat1">
<ListPreference
android:key="pref_list1"
android:title="@string/pref_list1"
android:dialogTitle="@string/pref_list1"
android:entries="@array/pref_list1_entries"
android:entryValues="@array/pref_list1_entries"
android:defaultValue="@string/pref_list1_default"/>
<EditTextPreference
android:key="pref_text2"
android:title="@string/pref_text2"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="Cat2"
android:key="pref_cat2">
<EditTextPreference
android:key="pref_text3"
android:title="@string/pref_text3"
/>
</PreferenceCategory>
显示 PreferenceFragment 时,一些分隔符会显示在首选项之间,但也会显示在每个 PreferenceCategory 的名称下方。 虽然我可以通过访问 PreferenceFragment 的 ListView 轻松修改首选项之间分隔线的颜色,但这对 PreferenceCategory 分隔线没有影响。
如何更改此类分隔线的颜色?
您有两个选择:
- 在您的应用主题中定义
listSeparatorTextViewStyle
请注意,依赖此主题属性的任何其他内容也将更改为使用您定义的样式。如果您不介意,它看起来像这样:
<style name="AppTheme" parent="android:...">
...
<item name="android:listSeparatorTextViewStyle">@style/ListSeparatorText</item>
</style>
<style name="ListSeparatorText" parent="android:Widget.TextView"><!--parent is optional -->
<item name="android:background">...</item>
...
</style>
- 为您的 PreferenceCategories 定义自定义布局
PreferenceCategory
的默认布局只是一个 TextView。您可以根据需要使布局尽可能简单或复杂,但某处应该是带有 android:id="@android:id/title"
的 TextView,以便自动绑定标题。
有了布局后,请在您的首选项中使用 android:layout
属性 xml:
<PreferenceCategory
android:title="Cat2"
android:key="pref_cat2"
android:layout="@layout/my_pref_category">
...
</PreferenceCategory>
或者,您可以在应用程序的主题中定义 preferenceCategoryStyle
,在这种情况下,您根本不需要根据自己的喜好使用 android:layout
xml。
<style name="AppTheme" parent="android:...">
...
<item name="android:preferenceCategoryStyle">@style/PreferenceCategoryStyle</item>
</style>
<style name="PreferenceCategoryStyle" parent="android:Preference.Category">
<item name="android:layout">@layout/my_pref_category</item>
...
</style>
我的 Prefernces
文件中也有同样的问题。我通过以下步骤解决了它:
1:定义文件名preferences_category.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+android:id/title"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<View style="@style/Divider" />
</LinearLayout>
2: 在样式 xml 文件中定义 style
:
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">@android:color/white</item>
</style>
3: 将 preferences_category.xml
文件作为 android:layout
属性添加到 preferences
xml 文件中::
<PreferenceCategory
android:title="My title"
android:layout="@layout/preferences_category">
现在您还可以在代码中为分隔线设置自定义可绘制对象。我不确定旧版本的支持库,但至少现在使用 AndroidX 它对我有用。
在您的PreferenceFragmentCompat
中您可以添加:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setDivider(getResources().getDrawable(R.drawable.your_divider_drawable, requireActivity().getTheme()));
}
您也可以调用 setDivider(null)
来删除分隔符。