如何更改PreferenceScreen中的PreferenceCategory分隔线的颜色(How to change the color of the PreferenceCategory div

编程入门 行业动态 更新时间:2024-10-25 08:15:13
如何更改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分隔线没有影响。

如何改变这些分隔线的颜色?

I have an android.support.v4.preference.PreferenceFragment which uses the following 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>

When displaying the PreferenceFragment, some dividers are shown between the preferences, but also under the name of each PreferenceCategory. Though I can easily modify the color of the dividers between the preferences by accessing the PreferenceFragment's ListView, this has no effect on the PreferenceCategory dividers.

How to change also the color of such dividers?

最满意答案

你有两个选择:

在应用程序的主题中定义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,以便自动绑定标题。

一旦你有一个布局,在你的首选项xml中使用android:layout属性:

<PreferenceCategory android:title="Cat2" android:key="pref_cat2" android:layout="@layout/my_pref_category"> ... </PreferenceCategory>

或者,您可以在应用的主题中定义preferenceCategoryStyle ,在这种情况下,您不需要在首选项xml中使用android:layout 。

<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>

You have two options:

Define the listSeparatorTextViewStyle in your app's theme

Note that anything else which relies on this theme attribute will also change to use the style you define. If that's ok with you, it will look something like this:

<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> Define a custom layout for your PreferenceCategories

The default layout for a PreferenceCategory is just a TextView. You can make your layout as simple or as complicated as you like, but somewhere should be a TextView with android:id="@android:id/title" so that the title is bound automatically.

Once you have a layout, use the android:layout attribute in your preference xml:

<PreferenceCategory android:title="Cat2" android:key="pref_cat2" android:layout="@layout/my_pref_category"> ... </PreferenceCategory>

Alternatively, you can define the preferenceCategoryStyle in your app's theme, in which case you don't need to use android:layout at all in your preference 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>

更多推荐

android,PreferenceFragment,PreferenceCategory,dividers,电脑培训,计算机培训,IT培训"/>

本文发布于:2023-08-01 21:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   颜色   分隔线   PreferenceScreen   divider

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!