【问题标题】: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>">
    

    这是在标题上方添加一个分隔线,因此您可以为除第一个类别之外的所有类别执行此操作。您可能需要调整分隔线的间距。

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 2016-07-16
      • 2015-03-01
      • 2016-06-06
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多