如何在Android中创建无边框按钮

dem*_*cmj 87 layout android button borderless

在Android的设计指南说,使用无边框按钮(见下图),但真的不解释如何.几个星期前有人问过同样的问题:如何创建标准的无边框按钮(如提到的设计指南)?并且有一个答案标记为"答案",但我仍然迷失了,我没有办法在一个已经"关闭"的问题上添加评论

答案者说

"看入主题属性buttonBarStyle, buttonBarButtonStyle以及borderlessButtonStyle"

但我仍然无法弄清楚如何实际使用这些东西.我用Google搜索了一下,找不到任何东西,所以我想我只是再问这个问题,希望有人可以提供一些更详细的信息.

在此输入图像描述

Pau*_*ond 144

几周前我在这里看到并注意到使用透明背景的答案时我觉得我解决了这个问题,但是它不够好,因为它会阻止按钮在按下时突出显示.

此外,将样式设置Widget.Holo.Button.Borderless为不合适,因为它使按钮边界变大.

为了一劳永逸地解决这个问题,我检查标准日历应用程序的android源代码,发现它使用以下内容:

android:background="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)

这样做可确保按钮无边框且尺寸正确.

  • selectableItemBackground仅支持API级别11,是否有旧版本的解决方案? (11认同)
  • 这是执行此操作的正确方法. (3认同)
  • 我一直在寻找这个已经有一段时间了.谢谢! (3认同)
  • 如何从Java代码而不是XML中完成?我在代码中创建一个ImageButton并希望它是无边框的,但在触摸时也会有高亮颜色 (3认同)
  • @alexandroid您必须为较低版本创建自己的<selector> (2认同)

小智 82

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

您Button或ImageButton标签上的属性:

    style="?android:attr/borderlessButtonStyle"
Run Code Online (Sandbox Code Playgroud)

  • 这适用于Buttons,但是当你为ImageButtons执行此操作时,虽然边框消失,但它失去了将其宽度仅包装到提供的图像的能力,并且开始表现得像具有最小宽度的普通按钮.所以对于ImageButtons,最好的方法是`android:background ="?android:attr/selectableItemBackground"` (2认同)

Win*_*der 23

如果你使用ActionbarSherlock ...

<Button 
  android:id="@+id/my_button" 
  style="@style/Widget.Sherlock.ActionButton" />
Run Code Online (Sandbox Code Playgroud)

  • 如果你使用HoloEverywhere` <Button android:background ="?selectableItemBackground"/>` (2认同)

Kar*_*som 19

前几天再次对此进行了标记.

我的解决方案:

这是通过两个步骤完成的:将按钮背景属性设置为android:attr/selectableItemBackground会创建一个带反馈但没有背景的按钮.

android:background="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)

将无边框按钮与其余布局分开的线由一个带有背景的视图完成:android:attr/dividerVertical

android:background="?android:attr/dividerVertical"
Run Code Online (Sandbox Code Playgroud)

为了更好地理解,这里是屏幕底部的OK/Cancel无边框按钮组合的布局(如上图右图所示).

<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>
Run Code Online (Sandbox Code Playgroud)


Mir*_*rko 13

这段代码适合我:

<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:background="?android:attr/dividerVertical" />

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:measureWithLargestChild="true"
    android:orientation="horizontal"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:paddingTop="0dip" >

    <Button
        android:id="@+id/cancel"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickCancel"
        android:text="@string/cancel" />

     <Button
        android:id="@+id/info"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickInfo"
        android:visibility="gone"
        android:text="@string/info" />

    <Button
        android:id="@+id/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickSave"
        android:text="@string/save" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我在底部显示3个按钮


Ric*_*ich 9

android:background="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)


jga*_*ani 8

<Button android:id="@+id/my_button" style="@android:style/Widget.Holo.Button.Borderless" />
Run Code Online (Sandbox Code Playgroud)