Is there an example of how to use a TouchDelegate in Android to increase the size of a view's click target?

后端 未结 11 563
误落风尘
误落风尘 2020-11-28 19:01

My understanding is that when you have a view that\'s too small to easily touch, you\'re supposed to use a TouchDelegate to increase the clickable region for that view.

11条回答
  •  攒了一身酷
    2020-11-28 19:14

    I asked a friend at Google and they were able to help me figure out how to use TouchDelegate. Here's what we came up with:

    final View parent = (View) delegate.getParent();
    parent.post( new Runnable() {
        // Post in the parent's message queue to make sure the parent
        // lays out its children before we call getHitRect()
        public void run() {
            final Rect r = new Rect();
            delegate.getHitRect(r);
            r.top -= 4;
            r.bottom += 4;
            parent.setTouchDelegate( new TouchDelegate( r , delegate));
        }
    });
    

提交回复
热议问题