2012-02-06 65 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