2012-02-06 68 views
77

Android Design Guidelines表示使用無邊框按鈕(請參閱下圖),但沒有真正解釋如何。幾個星期前,有人問這個問題:How to create standard Borderless buttons (like in the design guidline mentioned)?,並且有一個答案標記爲「答案」,但我仍然丟失,我沒有看到一種方式來添加評論到已關閉的問題如何在Android中創建無邊框按鈕

答案洱說

「看着主題屬性buttonBarStylebuttonBarButtonStyle,並borderlessButtonStyle

,但我仍然無法弄清楚如何實際使用這些東西。我搜索了一下,找不到任何東西,所以我想我會再次提出這個問題,希望有人可以提供一些關於它如何工作的更多細節。

enter image description here

回答

8
android:background="@android:color/transparent" 
+0

3個按鈕如果圖像背景不透明本身,它不會有太大的幫助。 – Gangnus 2012-02-06 22:02:57

+0

也android:background =「@ null」作品 – david72 2015-07-10 21:04:13

6
<Button android:id="@+id/my_button" style="@android:style/Widget.Holo.Button.Borderless" /> 
132

我想我已經解決了這個當我在幾周前看到這裏時,發現了使用透明背景的答案,但它不夠好,因爲它可以防止在按下按鈕時突出顯示按鈕。

此外,將樣式設置爲Widget.Holo.Button.Borderless並不合適,因爲它會使按鈕邊界變大。

要一次弄清楚了這一點,所有的,我檢查了Android源代碼的標準日曆應用,發現它使用了以下內容:

android:background="?android:attr/selectableItemBackground" 

這樣做,這樣保證了按鍵是無國界的正確的大小。

+3

這是這樣做的正確方法。 – roboguy12 2012-03-30 23:58:29

+3

我一直在尋找這個相當長一段時間了。謝謝! – Sandra 2012-05-15 14:25:21

+3

這怎麼能從java代碼而不是XML完成?我在代碼中創建了一個ImageButton,並希望它是無邊界的,但觸摸時也有突出顯示的顏色 – dsample 2012-06-22 14:23:17

63

看看這個:http://developer.android.com/guide/topics/ui/controls/button.html#Borderless

屬性您ButtonImageButton標籤:

style="?android:attr/borderlessButtonStyle" 
+0

那種讓視覺之外有點漣漪效應的風格呢?例如,在棒棒堂的聯繫人應用程序中,當您搜索聯繫人時,然後單擊「向上」按鈕(箭頭)?這就好像按鈕本身是圓形的,但有透明背景... – 2014-12-30 14:16:17

+1

@Dalc,真棒工作 – 2015-07-14 13:06:41

+0

@Dalc我怎樣才能應用它的編程..? – Moinkhan 2016-01-06 09:52:23

16

幾天前一個stumbeled在這個試。

這裏我的解決辦法:

這是在2個步驟完成:按鈕背景屬性設置爲機器人:ATTR/selectableItemBackground創建你的反饋,但沒有背景的按鈕。

android:background="?android:attr/selectableItemBackground" 

你佈局由視圖與背景機器人完成從靜止劃分國界的按鈕行:ATTR/dividerVertical

android:background="?android:attr/dividerVertical" 

爲了更好地理解這裏是一個佈局對於屏幕底部的確定/取消無邊界按鈕組合(如上面的右圖)。

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:layout_alignParentBottom="true"> 
     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dip" 
      android:layout_marginLeft="4dip" 
      android:layout_marginRight="4dip" 
      android:background="?android:attr/dividerVertical" 
      android:layout_alignParentTop="true"/> 
     <View 
      android:id="@+id/ViewColorPickerHelper" 
      android:layout_width="1dip" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="4dip" 
      android:layout_marginTop="4dip" 
      android:background="?android:attr/dividerVertical" 
      android:layout_centerHorizontal="true"/> 
     <Button 
      android:id="@+id/BtnColorPickerCancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_toLeftOf="@id/ViewColorPickerHelper" 
      android:background="?android:attr/selectableItemBackground" 
      android:text="@android:string/cancel" 
      android:layout_alignParentBottom="true"/> 
     <Button 
      android:id="@+id/BtnColorPickerOk" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:background="?android:attr/selectableItemBackground" 
      android:text="@android:string/ok" 
      android:layout_alignParentBottom="true" 
      android:layout_toRightOf="@id/ViewColorPickerHelper"/> 
    </RelativeLayout> 
24

如果使用ActionbarSherlock ...

<Button 
    android:id="@+id/my_button" 
    style="@style/Widget.Sherlock.ActionButton" /> 
+2

如果你使用HoloEverywhere的'

+0

完全適合我使用minimumSdk 9