2010-06-01 106 views
28

有時我的UI中有一個按鈕,它很小,很難點擊。到目前爲止,我的解決方案是在photoshop中的按鈕周圍添加一個透明邊框。只是增加按鈕上的填充不起作用,因爲這也會拉伸圖像。由於每次我想要改變可點擊的表面時打開photoshop是一件大事,有沒有辦法以編程的方式做到這一點?我已經嘗試在按鈕後面放置一個框架佈局,並使其可點擊,但隨後該按鈕不會改變觸摸的外觀,因爲它應該。當然,我還可以在framelayout上添加一個ontouchlistener,它可以改變按鈕的外觀,但如果我有幾個這樣的按鈕,那麼它就是一些代碼。我可以通過編程方式增加按鈕onclick-area嗎?

乾杯,

回答

29

我剛剛發現了一個巧妙的方法來解決這個問題。

  1. 將按鈕用一個說法放在圍繞按鈕的一個LinearLayout中,該按鈕帶有填充四周的按鈕。
  2. 將相同的onclick添加到按鈕的LinearLayout。
  3. 在按鈕中將duplicateParentState設置爲true,當按鈕在按鈕外部但在LinearLayout內部單擊時,該按鈕將突出顯示。

    <LinearLayout 
        android:layout_height="fill_parent" 
        android:onClick="searchButtonClicked" 
        android:layout_centerVertical="true" 
        android:orientation="horizontal" 
        android:layout_width="wrap_content" 
        android:paddingRight="10dp" 
        android:paddingLeft="30dp"> 
        <Button 
         android:id="@+id/search_button" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="@drawable/toggle_button_selector" 
         android:textColor="#fff" 
         android:text="Search" 
         android:focusable="true" 
         android:textStyle="bold" 
         android:onClick="searchButtonClicked" 
         android:layout_gravity="center_vertical" 
         android:duplicateParentState="true"/> 
    </LinearLayout> 
    
+5

這隻適用於sortof。爲了使它完全工作,你需要使按鈕setClickable(false),否則單擊botton本身不會突出顯示,因爲它從父節點處獲得它的狀態,此時未被點擊。 一旦你使按鈕不可點擊,就不需要在其上設置onClick監聽器。 – 2013-01-16 03:50:50

2

我認爲你的解決方案是目前可用的最好的解決方案,如果你不想深入一些android的東西,並自己攔截所有的motionEvent和TouchEvents,那麼你也需要觸發按下的視圖你自己的按鈕等。

只需創建一個帶有可拉伸透明邊框的nine patch image。通過這種方式,您可以更改圖像的大小而無需更改圖像本身,並且您的按鈕會隨着顯示背景的變化而變大或縮小。

33

我個人,我會使用TouchDelegate。這可以讓你處理觸摸目標,並將視覺界限視爲兩個不同的東西。非常方便...

http://developer.android.com/reference/android/view/TouchDelegate.html

+0

感謝GeorgeLawrence,你已經在stackoverflow上發表了一篇文章,並且我正在尋找它! – 2011-04-28 15:03:23

+0

這通常是觸摸區域問題的最佳解決方案。 – 2011-08-20 01:27:43

+3

關於'TouchDelegate'和一段代碼的解釋:http://developer.android.com/training/gestures/viewgroup.html#delegate – 2014-07-08 10:46:27

1

Anothe想法是讓新的透明圖像,並把它圖標,觸摸區域會越來越設計看起來很完美。 Check out the image

謝謝。

0

到位保證金只需提供填充佈局

<ImageView 
    android:id="@+id/back_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="25dip" 
    android:src="@drawable/back_btn" /> 
+1

這將擴大視圖的背景,這可能不是所期望的。 – pgsandstrom 2012-10-03 11:23:16

+0

是的,這是正確的,但在佈局和按鈕很少的情況下,它可能會有所幫助。 – sidhanshu 2012-10-04 05:45:45

+0

如果不希望圖像被拉伸,則填充不夠,必須使用'android:scaleType =「centerInside」'以及填充以獲得所需的效果。 – ForceMagic 2014-02-06 18:49:00

12

這是非常晚的「跟風」,但來這和尋找一個解決其他問題後,我發現了一個簡單,優雅我自己的解決方案。

另一個question抱怨說他們的圖片的透明背景是不可點擊的。如果這是一個問題,這似乎也可以解決這個問題。

這裏的按鈕:

<ImageButton 
    android:id="@+id/arrowUp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/arrow_up" 
    android:background="@drawable/clear_button_background" /> 

相關的代碼行是最後兩個。 "@drawable/arrow_up"是在可抽拉* .xml文件定義如下* .png文件的幾個按鈕狀態:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:state_pressed="true" 
     android:drawable="@drawable/tri_up_blue" /> <!-- pressed --> 
<item android:state_selected="true" 
     android:drawable="@drawable/tri_up_blue" /> <!-- selected --> 
<item android:state_focused="true" 
     android:drawable="@drawable/tri_up_blue" /> <!-- focused --> 
<item android:drawable="@drawable/tri_up_green" /> <!-- default --> 
</selector> 

只要你的基本按鈕。沒什麼特別的。而"@drawable/clear_button_background"僅僅是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@android:color/transparent"/> 
    <size android:width="20dp" android:height="30dp" /> 
</shape> 

高度和寬度這裏是點擊區域,根據需要調整。您可以在單個視圖中重複使用此按鈕,因爲您需要在單個視圖中使用該按鈕,而不像荒謬的TouchDelegate。沒有額外的聽衆。它不會將任何視圖或組添加到您的層次結構中,並且您不會整天干擾填充和邊距。

簡單。優雅。

+3

這實際上非常整潔,謝謝! – pgsandstrom 2013-01-14 11:49:24

+0

好主意,如果你可以使用ImageButton,但很多按鈕是基於文本的,這使得這更難以應用。 – greg7gkb 2015-04-30 20:26:10

+0

@ greg7gkb爲基於文本的按鈕,您只需添加'padding'。 (將填充添加到基於圖像的按鈕會拉伸圖像,這就是人們尋找除填充以外的其他解決方案的原因。) – ToolmakerSteve 2015-10-07 01:36:20

相關問題