search

Home  >  Q&A  >  body text

Android中LinearLayout的点击事件如何包含内部的控件?

一个LinearLayout里面有一个ImageView和一个TextView,想在点击这个Layout的时候执行一个事件,于是给LinearLayout设置了onClickListener,但发现只有在点击ImageView之外部分、LinearLayout之内的部分时,才会响应点击操作,是不是ImageView把LinearLayout盖住的部分就不响应父控件LinearLayout的点击事件了呢。

如何才能让点击ImageView的时候也响应这个LinearLayout的点击事件呢,只能再给ImageView绑定相同的监听器吗?

天蓬老师天蓬老师2803 days ago729

reply all(3)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:22:20

    LinearLayout中添加android:descendantFocusability=”blocksDescendants”

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 17:22:20

    As you asked, just call linearlayout.performclick() in the onclick method of imagview.

    reply
    0
  • 阿神

    阿神2017-04-17 17:22:20

    Inherit LinearLayout, such as MyLinearLayout, and then override onInterceptTouchEvent. The return value is still false, indicating that the event is not will be truncated (truncation means that the event will still be passed to the onTouchEvent method of ViewGourp) LinearLayout,比如MyLinearLayout,然后覆写onInterceptTouchEvent,返回值依然用false,表示事件不会被截断(截断就是说事件依然会传给ViewGourponTouchEvent方法)

    然后你就可以在onInterceptTouchEvent干你想干的事情,不管你点这个LinearLayout

    Then you can do what you want to do in onInterceptTouchEvent. No matter where you click in this LinearLayout, this method will be called first.


    Reference:

    https://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)🎜

    reply
    0
  • Cancelreply