【发布时间】:2012-05-01 21:11:15
【问题描述】:
我有一个首选项屏幕,其中填充了数据库中的项目。我通过创建自己的PreferenceActivity 来完成这项工作。在活动中,我创建DialogPreference 项目并将它们添加到我的PreferenceCategory 为了在屏幕上设置首选项的样式,我使用自定义布局并使用setLayoutResource(R.layout.custom_pref_row) 应用它
这基本上将ImageButton 添加到与布局右侧对齐的视图中。这一切都很好,我的偏好屏幕显示带有按钮的自定义视图。我的问题是如何将点击侦听器附加到自定义视图中的按钮?我无法找到从PreferenceActivity 获取行的View 的方法。如果我的项目不是动态创建的,我也许可以从 XML 中完成这一切,然后引用 id 或按钮,但我可以这样做,因为我正在动态创建列表。
关于如何处理每个项目的ImageButton 有什么建议吗?最后我想配置按钮以启动删除确认对话框。
R.layout.custom_pref_row:
<?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" />
<ImageButton android:id="@+id/pref_delete_station" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_trash_can" android:layout_alignParentRight="true" android:background="@null"></ImageButton>
</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>
我的 PreferenceActivity 的相关部分:
DialogPreference diaPref;
for (Station mStation : sList) {
diaPref = new StationEditor(this.getPreferenceScreen().getContext(), null, this, mStation);
diaPref.setLayoutResource(R.layout.custom_pref_row);
diaPref.setTitle(mStation.getName());
diaPref.setKey(STATION_PREFIX + mStation.getId());
// add new preference
stationTypesCategory.addPreference(diaPref);
}
【问题讨论】:
标签: android layout preferences