我有这个xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="20dip"
android:gravity="center_horizontal"
android:text="@string/app_name"
android:textSize="25dip"
android:textStyle="bold"
android:typeface="serif" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="150dip"
android:layout_marginBottom="10dip"
android:contentDescription="@string/iv_restaurantImage"
android:src="@drawable/ic_launcher" />
<ListView
android:id="@+id/lv_restaurant_description_information"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
,列表视图是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dip"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:typeface="serif"
android:textSize="15dip" />
<TextView
android:id="@+id/tv_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="@string/IV_LOGO_DESCRIPTION"
android:textSize="20dip" />
</LinearLayout>
,listview标题是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/rl_simple_list_item_header"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/orderMeal"
android:layout_marginLeft="20dip"
android:id="@+id/tv_restaurant_description_orderMeal"
android:drawableTop="@drawable/order_meal" />
<TextView
android:id="@+id/iv_simple_list_item_header_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/favorite"
android:layout_alignParentRight="true"
android:layout_marginRight="20dip"
android:drawableTop="@drawable/favorite"/>
</RelativeLayout>
在java活动中,我将标题添加到listview中,如此
lv_restaurantInformation = (ListView) findViewById(R.id.lv_restaurant_description_information);
View header = LayoutInflater.from(this).inflate(
R.layout.simple_list_item_header, null);
tv_favorite = (TextView) header
.findViewById(R.id.iv_simple_list_item_header_favorite);
lv_restaurantInformation.addHeaderView(header);
一切都很好,应用程序正在运行,现在我想获取listview标题上的元素,怎么样?以及如何为他们设置onclick监听器?
答案 0 :(得分:2)
看起来你已经在做了。
tv_favorite = (TextView) header
.findViewById(R.id.iv_simple_list_item_header_favorite);
// now just add a click listener.
tv_favorite.setOnClickListener(new View.OnClickListener() .... );