2011-12-01 68 views
0

我在LinearLayout中顯示一個小圖像,右側和左側有一些邊距。 我的問題是保證金是不可點擊的。 如何在我的視圖的左側和右側顯示空格,使用此額外空間來擴展可點擊區域?Android:展開ImageView可點擊區域

<ImageButton 
    android:id="@+id/markReadButton" 
    android:layout_width="22dp" 
    android:layout_height="22dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp" 
    android:adjustViewBounds="true" 
    android:background="@android:drawable/checkbox_on_background" 
    android:gravity="center_vertical" 
    android:onClick="onMarkRead" > 
</ImageButton> 

回答

0

我認爲使用填充而不是邊距可以讓您的物品有更多的空間,但也可以讓它點擊。

看一看利潤率的描述VS填充:Difference between a View's Padding and Margin

編輯: 嘗試使用機器人:SRC的圖像,設置機器人:背景=「@空」,增加的總體規模ImageButton對象。那時候,我甚至不確定你會需要填充。您可以根據需要使用ImageButton中的src圖像修改高度和寬度。

<ImageButton 
     android:id="@+id/markReadButton" 
     android:layout_width="60dp" 
     android:layout_height="40dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:src="@android:drawable/checkbox_on_background" 
     android:background="@null" 
     android:adjustViewBounds="true" 
     android:onClick="onMarkRead" 
    /> 
+0

我試過了,它不給我任何空間... – user1026605

+0

看起來有一些額外的東西必須做,以獲得填充像你一樣工作想。您可以使用src值設置圖像並將背景設置爲@null。然後增加ImageButton對象的整體寬度將爲您提供所需的大小。 – Aldryd

1

這是在Google IO 2011 Schenduling應用程序中使用TouchDelegate完成的。看看像這樣使用它的Activity;

// Larger target triggers star toggle 
final View starParent = mRootView.findViewById(R.id.header_session); 
FractionalTouchDelegate.setupDelegate(starParent, mStarred, new RectF(0.6f, 0f, 1f, 0.8f)); 

The TouchDelegate implementation is implemented here。它是開源的,應該很容易導入到你的項目中。

祝你好運,希望這有助於!

0

只需添加布局父,並設置如20dp寬度和高度

<RelativeLayout android:id="@+id/relativeLayoutButton" 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:paddingRight="20dp" 
     android:paddingLeft="20dp" > 

<ImageButton 
    android:id="@+id/markReadButton" 
    android:layout_width="22dp" 
    android:layout_height="22dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp" 
    android:adjustViewBounds="true" 
    android:background="@android:drawable/checkbox_on_background" 
    android:gravity="center_vertical" 
    android:onClick="onMarkRead" > 
</ImageButton> 

更大的規模和毗鄰佈局和圖像的相同posision。 設置也可點擊相同的佈局和按鈕