我的应用程序中有一个垂直的 LinearLayout,其中每一行都应该有一个更复杂的 subview ,其中包含图像和两行文本。现在我想让整行可单击以开始其他操作。这意味着整个事情应该对点击使用react:包含其他元素、图像甚至两行文本的基本 View 。
我的问题:最好的方法是什么?我无法对整行使用简单的按钮,并且据我所知,我无法为 View /图像/标签设置 onClick-Handler?
知道如何做到这一点吗?
谢谢!
最佳答案
最简单的选择是将 android:descendantFocusability="blocksDescendants"
放在父 View 上,这将阻止任何 subview 从父 View 中获取点击,然后在父 View 上放置一个点击监听器仅有的。
这是假设您不关心用户在父 View 中单击了哪个 subview 。
<LinearLayout
android:id="@+id/parent"
...
android:clickable="true"
android:descendantFocusability="blocksDescendants" >
<TextView
... />
<ImageButton
... />
</LinearLayout>
并在代码中:
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
parent.setOnClickListener(...);
关于java - 使子元素可点击的整个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46047453/