Android无边框按钮



我不想成为第三个问这个问题的人,但前两个问题似乎还没有完全回答。android设计指南详细介绍了无边框按钮,但没有说明如何制作。在之前的一个答案中,有一个建议可以使用:

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

这适用于Holo主题,但我使用了很多Holo。光以及

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" />

只需在Button标签中添加以下样式属性:

style="?android:attr/borderlessButtonStyle"

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

在这里查看我的答案。简而言之,正确的解决方案是使用以下内容:

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

冒着打死马的风险,这里有另一个(可能更好)选择。AppCompat v7定义了一种无边框按钮样式。如果你已经在使用AppCompat库,那么就使用它:

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

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

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

如果有人正在寻找轻量级的解决方案来获得不支持Holo主题的API的无边界按钮(就像我做了很长一段时间),我会在下面发布我的解决方案:

<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>

你所要做的就是改变颜色和文字以适应你自己的背景。祝你一切顺利!

最新更新