TouchDelegate 不会增加 TextView 上的触摸区域



我想知道我是否正确设置了触摸委托。 我想在我的 LinearLayout 中增加 TextView 的触摸区域。

我的布局如下所示。

<LinearLayout
        android:id="@+id/subcategory"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:background="#000000"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="5dp" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="All"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="None"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
</LinearLayout>

我从onActivityCreated()调用以下内容,其中subCategoryLayout是LinearLayout

subCategoryLayout.post(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < subCategoryLayout.getChildCount(); i++) {
                        Rect delegateArea = new Rect();
                        TextView d = (TextView) subCategoryLayout.getChildAt(i);
                        d.getHitRect(delegateArea);
                        delegateArea.bottom += 50;
                        delegateArea.top += 200;
                        delegateArea.left += 50;
                        delegateArea.right += 50;
                        TouchDelegate expandedArea = new TouchDelegate(delegateArea, d);
                        subCategoryLayout.setTouchDelegate(expandedArea);
//                        if (View.class.isInstance(d.getParent())) {
//                            ((View) d.getParent()).setTouchDelegate(expandedArea);
//                        }
                    }
                }
            });

第一点是 View 只能有一个 TouchDelegate。因此,在您的 for 循环之后,实际上最后一个 TouchDelegate 被设置为 subCategoryLayout。如果要将多个 TouchDelegate 添加到一个视图中,可以使用 TouchDelegateComposite

第二点是您希望将触摸区域扩展到顶部的50dp和底部的200dp。但是您的子类别布局高度为 35dp,只有那些命中子类别布局的触摸才会传递给 TouchDelegate。因此,如果要将触摸区域扩展到更大的值,则必须将TouchDelegate添加到一些足够大的父布局中。如果你采用这种方式,你应该记住,你必须相对于更大的布局计算委托面积。

最新更新