【问题标题】:Add footer divider in PreferenceFragment在 PreferenceFragment 中添加页脚分隔符
【发布时间】:2016-02-26 16:44:15
【问题描述】:
我创建的首选项屏幕示例:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="GROUP1">
<Preference
android:title="ITEM1"
android:key="ITEM1">
</Preference>
</PreferenceCategory>
<PreferenceCategory android:title="GROUP2">
<Preference
android:title="ITEM2"
android:key="ITEM2">
</Preference>
</PreferenceCategory>
</PreferenceScreen>
我想在每个 PreferenceCategory 之后添加一个页脚分隔符。
【问题讨论】:
标签:
android
preferencefragment
【解决方案1】:
您可以为您的偏好类别创建自定义布局:
<?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">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#808080" />
<TextView
android:id="@android:id/title"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
</LinearLayout>
然后在您的首选项 XML 文件中,您引用该布局:
<PreferenceCategory
android:title="Preference Title"
android:layout="@layout/<the layout name above>">
这是在标题上方添加一个分隔线,因此您可以为除第一个类别之外的所有类别执行此操作。您可能需要调整分隔线的间距。