2012-03-14 143 views
20

我讨厌成为第三个问这个问题的人,但之前的twoaskings似乎还没有完全回答。 Android设计准则详述borderless buttons,但不是如何制作它们。在前面的答案之一,有一个sugestion使用方法:Android无边界按钮

style="@android:style/Widget.Holo.Button.Borderless"

这非常适用于一个全息主题,但我用了很多Holo.Light,以及和

style="@android:style/Widget.Holo.Light.Button.Borderless"

似乎并不存在。有没有办法在Holo.Light中为无边框按钮应用样式,或者更好,只需应用无边框标签而不指定它属于哪个主题,以便应用程序可以在运行时选择适当的样式?

Holo.ButtonBar似乎符合我正在寻找的法案,除非它没有提供任何用户反馈,它已被按下。

另外,在文档中是否有一个地方列出了可应用的样式以及对它们的描述?不管我有多少谷歌,并通过文档搜索,我找不到任何东西。如果我右键单击并编辑样式,则只有一个非描述性列表。

任何帮助,将不胜感激。

编辑:从javram得到了一个完美的答案,我想为任何有兴趣添加谷歌已采用的部分边框的人添加一些XML。

佛拉水平分隔,这个伟大的工程

<View 
    android:id="@+id/horizontal_divider_login" 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:background="@color/holo_blue" /> 

这对于一个垂直:

<View 
    android:id="@+id/vertical_divider" 
    android:layout_width="1dip" 
    android:layout_height="match_parent" 
    android:layout_marginBottom="8dp" 
    android:layout_marginTop="8dp" 
    android:background="@color/holo_blue" /> 

回答

21

检查出我的回答here。简而言之正确的解决方法是使用以下命令:

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

当您使用Android 2.2,2.3.x时,它不起作用:( – Xdg 2012-10-07 05:50:06

+0

@Xdg在使用HolloEveywhere时会起作用:https://github.com/Prototik/HoloEverywhere/ – 2013-05-19 06:19:15

+5

如果您使用AppCompat库,然后删除“android:”前缀,它将工作: android:background =“?attr/selectableItemBackground” – 2014-05-29 10:21:48

8

此代码将删除所有背景的按钮:

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

这似乎是一个答案是多简单的拿3个问题的回答,但它究竟是做的伎俩。 此外,对于任何人后来看到这一点,想知道,为了获得谷歌的半边框已采纳,我添加了一些代码,在第一篇文章中为我工作,因为在子响应中发布代码很难阅读。 – 2012-03-14 04:58:06

+0

我记得当我试图找出这个问题时,我想到了同样的事情。我用它作为一个超链接,本应该是一个相对简单的任务,或者所以我认为... – javram 2012-03-14 05:00:01

+0

这需要api 11 – gsscoder 2012-11-23 08:50:31

38

只需添加下面的样式属性,在Button标签:

style="?android:attr/borderlessButtonStyle" 

来源:http://developer.android.com/guide/topics/ui/controls/button.html#Borderless

+4

这是一个很好的答案,但请记住,该属性添加API 11. – 2013-03-06 02:33:58

+0

而borderlessButtonStyle将background设置为selectableItemBackground并设置一些填充,仅此而已。 – Anderson 2014-04-17 04:43:24

1

只是柜面任何人正在寻找轻量级的解决方案,以获得不支持Holo主题的API的无边界按钮(就像我很长一段时间一样),Im在下面发布我的解决方案:

<View 
    android:layout_height="1dp" 
    android:layout_width="match_parent" 
    android:background="#505154" 
    android:layout_marginLeft="10dp" android:layout_marginRight="10dp"/> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/btn_Reply" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="@android:color/transparent" 
     android:paddingBottom="15dp" android:paddingTop="15dp" 
     android:textColor="#5c5966" 
     android:text="@string/btnReplyText"/> 

    <View 
     android:layout_height="fill_parent" 
     android:layout_width="1dp" 
     android:background="#505154" 
     android:layout_marginBottom="7dp" android:layout_marginTop="7dp"/> 

    <Button 
     android:id="@+id/btn_Delete" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:paddingBottom="15dp" android:paddingTop="15dp" 
     android:textColor="#5c5966" 
     android:background="@android:color/transparent" 
     android:text="@string/btnDeleteText" /> 

    <View 
     android:layout_height="fill_parent" 
     android:layout_width="1dp" 
     android:background="#505154" 
     android:text="@string/btnReplyText" 
     android:layout_marginBottom="7dp" android:layout_marginTop="7dp" /> 

    <Button 
     android:id="@+id/btn_Exit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:text="@string/btnExitText" 
     android:paddingBottom="15dp" android:paddingTop="15dp" 
     android:textColor="#5c5966" 
     android:background="@android:color/transparent"/> 
</LinearLayout> 

您只需更改颜色和文字以适合自己的背景。祝一切顺利!

6

冒着死马的危险,这里有另一个(可能更好)选项。 AppCompat v7定义了无边界按钮样式。如果您已经利用该程序兼容性库,只是去与:

<Button android:text="@string/borderlessButtonText" style="@style/Widget.AppCompat.Button.Borderless" />