我有一个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>
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>
我的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
xml文件中的preferences
属性::
<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)
删除分隔符。