Android 为首选项创建自定义布局

Android 为首选项创建自定义布局,android,Android,我正在尝试创建一个用于管理首选项的自定义布局。我知道PreferenceActivity提供了标准和推荐的布局,但是如果我想添加,比如说,一个按钮,我该如何获得它 我计划设计应用程序,以便当用户触摸主活动屏幕上的某个项目时,它会进入用户可以启用的选项的“子菜单”,然后出现在下一个屏幕上(它必须是线性的)。用户每次使用应用程序时都必须设置这些参数。目前,我正在考虑使用标准的PreferenceActivity来表示这个选项子菜单 也许这不是正确的方法 编辑:我在谷歌搜索时忽略了解决方案 您始终可以

我正在尝试创建一个用于管理首选项的自定义布局。我知道
PreferenceActivity
提供了标准和推荐的布局,但是如果我想添加,比如说,一个
按钮
,我该如何获得它

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

也许这不是正确的方法


编辑:我在谷歌搜索时忽略了解决方案

您始终可以创建自定义首选项布局项,并在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>
之后,只需在活动代码中启动
findViewById
,并在按钮上附加一个侦听器。如果活动中有多个按钮,可能需要做更多的工作,但这不应该是不合理的

您可以在此处找到源代码:

我计划设计应用程序,以便当用户触摸主活动屏幕上的某个项目时,它会进入用户可以启用的选项的“子菜单”,然后出现在下一个屏幕上(它必须是线性的)


这听起来像一个嵌套的
首选项屏幕

谢谢!但我找到了一个更优雅的解决方案来解决我最初忽视的问题。实际上,这个解决方案似乎比使用listView的解决方案更灵活(在对这个答案的第一条评论中提到)。使用此解决方案,您可以将自定义视图元素与标准首选项窗口小部件对齐并混合在一起-另一种解决方案只允许在首选项布局之前或之后进行自定义布局。向其中添加自定义布局有何意义。这难道不完全等同于采用定制布局并将其膨胀为任何片段或活动吗?如果我错了,请更正。如果从“活动”调用findViewById,则返回null。在片段中没有这样的方法。如何访问自定义首选项布局中的其他视图?找到使用findViewById()的方法了吗?
<Preference
    android:title="@string/label_pref_version"
    android:key="@string/pref_version"
    android:layout="@layout/pref" />