为偏好设置创建自定义布局

47

我正在尝试创建一个自定义布局来管理首选项。我知道有PreferenceActivity提供的标准推荐布局,但如果我想添加一个Button,该怎么办呢?

我计划设计应用程序,使用户触摸主活动屏幕上的某个项目时,它进入到用户可以启用的“子菜单”选项中,随后出现在下一个屏幕上(必须按照这种线性进度)。用户每次使用应用程序都必须设置这些选项。目前,我正在考虑使用标准的PreferenceActivity来表示这些选项的子菜单。

也许这不是正确的方法?

编辑:我在Google搜索时忽略了解决方案。如何向PreferenceScreen添加按钮

2个回答

86

您可以始终创建一个自定义的偏好设置布局项,并在PreferenceActivity中使用它。例如,我就是这样做的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="2" />

        <ImageView android:id="@+id/ImageView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/go"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>

虽然有一些浪费,但基本上创建了两行文本(标题、副标题),右侧带有一张图片。你可以将ImageView替换为Button,就可以完成了。然后在xml/prefs.xml文件中设置单个首选项的布局,如下所示:

<Preference
    android:title="@string/label_pref_version"
    android:key="@string/pref_version"
    android:layout="@layout/pref" />

在Activity代码中使用findViewById查找并附加监听器到按钮上。如果在Activity中有多个按钮,可能需要进行更多的工作,但不应该是不合理的。

你可以在这里找到源代码:https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/preference.xml


2
谢谢!但我找到了一个更优雅的解决方案来解决我一开始忽略的问题。https://dev59.com/DXE85IYBdhLWcg3wmExW - kibibyte
2
这个解决方案似乎更加灵活 - 与在此答案的第一条评论中提到的使用listView的解决方案相比。通过这个解决方案,您可以将自定义视图元素放在一行内,并混合使用标准首选项小部件 - 另一个解决方案只允许在首选项布局之前或之后使用自定义布局。 - Gene Bo
将自定义布局添加到<preference...>有什么意义呢?难道它不完全相同于将该自定义布局填充到任何片段或活动中吗?如果我错了,请纠正我。 - Alex
1
如果在Activity中调用findViewById,则返回null。在fragment中没有这样的方法。我如何访问自定义首选项布局中的附加视图? - lxknvlk
我目前也遇到了同样的问题。 - undefined
显示剩余3条评论

13
这听起来像是一个嵌套的PreferenceScreen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接