我正在尝试创建一个用于管理首选项的自定义布局.我知道有标准和推荐的布局PreferenceActivity,但如果我想添加,比方说,Button我怎么能得到它?
我计划设计应用程序,以便当用户触摸主活动屏幕上的某个项目时,它会转到用户可以启用的选项的"子菜单",随后会出现在下一个屏幕上(它必须在此线性进展).用户每次使用该应用程序时都必须设置这些.目前,我正在考虑使用该标准PreferenceActivity来表示这个选项的子菜单.
也许这不是正确的方法?
编辑:我在谷歌搜索时忽略了解决方案.如何向PreferenceScreen添加按钮
Fem*_*emi 71
您始终可以创建自定义首选项布局项并在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>
Run Code Online (Sandbox Code Playgroud)
它有一些浪费,但基本上创建了2行(标题,副标题)和右侧的图像.你可以用一个Button替换ImageView,你就可以了.然后,在xml/prefs.xml文件中设置个人首选项的布局,如下所示:
<Preference
android:title="@string/label_pref_version"
android:key="@string/pref_version"
android:layout="@layout/pref" />
Run Code Online (Sandbox Code Playgroud)
之后,只需激活findViewById您的Activity代码并将监听器附加到按钮.如果活动中有多个按钮,可能还需要做更多的工作,但不应该是不合理的.
您可以在此处找到源代码:https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/preference.xml
Com*_*are 13
我计划设计应用程序,以便当用户触摸主活动屏幕上的某个项目时,它会转到用户可以启用的选项的"子菜单",随后会出现在下一个屏幕上(它必须在此线性进展).
这听起来像是嵌套的PreferenceScreen.