Ответ 1
Вы всегда можете создать пользовательский элемент макета предпочтений и использовать его в 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>
У него есть отходы, но в основном создает 2 строки (заголовок, субтитры) с изображением справа. Вы можете заменить ImageView на кнопку, и все будет готово. Затем вы устанавливаете макет для индивидуального предпочтения в файле xml/prefs.xml
следующим образом:
<Preference
android:title="@string/label_pref_version"
android:key="@string/pref_version"
android:layout="@layout/pref" />
После этого просто стреляйте findViewById
в свой код активности и присоедините прослушиватель к кнопке. Возможно, вам придется сделать еще одну работу, если в Activity есть несколько кнопок, но не должно быть необоснованным.
Здесь вы можете найти исходный код: https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/preference.xml