2012-12-10 35 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。