android 如何让上层ConstraintLayout 点击事件不透传
要让上层的 ConstraintLayout 不透传点击事件,可以在上层的 ConstraintLayout 上设置一个空的点击事件监听器,阻止点击事件的传递。具体步骤如下:
- 在上层的 ConstraintLayout 的 XML 布局文件中,给 ConstraintLayout 添加一个空的点击事件监听器:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/upperLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onUpperLayoutClicked">
<!-- other views -->
</androidx.constraintlayout.widget.ConstraintLayout>
- 在与上层 ConstraintLayout 相关的 Activity 或 Fragment 中,添加一个空的点击事件处理方法:
public void onUpperLayoutClicked(View view) {
// do nothing
}
这样,当点击上层 ConstraintLayout 时,点击事件将被上层的空的点击事件监听器处理,从而阻止了点击事件的传递。
原文地址: https://www.cveoy.top/t/topic/i5TC 著作权归作者所有。请勿转载和采集!