【发布时间】:2014-04-08 11:52:10
【问题描述】:
所以,我有一个如下定义的标题视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<TextView android:id="@+id/purchase_menu_header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:text="@string/purchase_options_label"/>
</LinearLayout>
膨胀如下:
View header = ((Activity) context).getLayoutInflater().inflate(R.layout.purchase_menu_header, options, false);
options.addHeaderView(header);
我对选项 ListView 中的项目有一个点击监听器:
options.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Offer offer = (Offer) options.getItemAtPosition(position);
initiatePurchase(offer.getStoreProduct());
}
});
以及标题视图本身的点击监听器:
header.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
baseLayout.removeView(background);
baseLayout.removeView(overlay);
}
});
我的问题是标题只记录对标题文本部分的点击。期望的行为是让整个标题可点击。我已经尝试过使用 setOnItemClickListener 和 getItemAtPosition(0) 等解决方案,但这会返回 null。还尝试使用 MATCH_PARENT 扩展标题中的文本视图,但没有运气。
【问题讨论】:
-
您是否尝试过删除 LinearLayout,即您的布局文件是否只包含 TextView?然后,您可以调整 TextView 的重力和/或填充以获取所需的文本。
-
我也会在标题中添加一张图片,所以我认为这个解决方案不会很好。
标签: android android-layout android-listview