2012-12-10 37 views
0

我想知道是否正确设置了TouchDelegate。我想增加我的LinearLayout中TextView的触摸区域。TouchDelegate不增加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); 
//      } 
        } 
       } 
      }); 
+0

是你的问题解决?我有同样的问题。 – hshed

+0

使用触摸代表实际上并没有增加触摸区域 – heero

回答

0

的第一点是,查看只能有一个TouchDelegate。所以在你的for循环之后,最后一个TouchDelegate被设置为subCategoryLayout。如果您想添加几个TouchDelegates到一个视图,您可以使用TouchDelegateComposite

第二点是您想要将触摸区域扩展为顶部50dp和底部200dp。但是你的subCategoryLayout高度是35dp,只有触及subCategoryLayout的触摸才会传递给TouchDelegate。因此,如果您想要将触摸区域扩大到更大的值,则必须将TouchDelegate添加到某个尺寸足够大的父级布局。如果你这样做,你应该记住,你必须相对于更大的布局来计算delegateArea。