没有在子级的LinearLayout上触发onClick [英] onClick not triggered on LinearLayout with child
本文介绍了没有在子级的LinearLayout上触发onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有较小TextView子级的自定义LinearLayout.我希望能够单击TextView未覆盖的区域,因此我将clickable = true和onclicklistener设置为LinearLayout,但不会触发onClick.如果我在TextView上设置onclick侦听器,它将按预期工作...
I've got a custom LinearLayout with a smaller TextView child. I'd like to be able to click the area not covered by the TextView, so I set clickable=true and an onclicklistener to the LinearLayout, but onClick is not triggered. If I set the onclick listener on the TextView it works as expected...
有人可以帮忙吗?
ar_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ar_item" android:layout_width="202dp"
android:layout_height="62dp" android:background="@drawable/bg_item_ar"
android:clickable="true">
<TextView android:id="@+id/ar_item_txt"
android:layout_width="164dp" android:layout_height="fill_parent"
android:paddingBottom="8dp" android:paddingLeft="8dp"
android:paddingTop="8dp" android:paddingRight="6dp" android:gravity="center"
android:background="#50000000" />
</LinearLayout>
我的自定义LinearLayout
public class ARView extends LinearLayout
{
public ARView(final Context context, String name, String id)
{
super(context);
getLayoutInflater().inflate(R.layout.ar_item, this ,true);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ar_item, null);
TextView textView = (TextView) findViewById(R.id.ar_item_txt);
textView.setText(name);
setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Toast t = Toast.makeText(context, "hey!", Toast.LENGTH_SHORT);
t.show();
}
});
}
}
推荐答案
为每个孩子
android:duplicateParentState ="true"
android:duplicateParentState="true"
这篇关于没有在子级的LinearLayout上触发onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文