首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何创建标准的无边框按钮(如前面提到的设计指南)?

如何创建标准的无边框按钮(如前面提到的设计指南)?
EN

Stack Overflow用户
提问于 2012-01-14 03:03:32
回答 19查看 120.8K关注 0票数 117

我只是在检查设计指南,并对无边框按钮感到好奇。我睁大眼睛,试图在源代码中找到它,但我自己无法将其整合在一起。这是普通的Button小部件,但您添加了自定义(Android默认)样式吗?如何使这些无边框按钮(当然你可以将背景设置为空,但我没有分隔符)?

此处提供设计指南的链接:

EN

回答 19

Stack Overflow用户

回答已采纳

发布于 2012-07-01 09:54:10

要消除一些混淆,请执行以下操作:

这分两步完成:将按钮背景属性设置为android:attr/selectableItemBackground将创建一个具有反馈但没有背景的按钮。

代码语言:javascript
复制
android:background="?android:attr/selectableItemBackground"

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

代码语言:javascript
复制
android:background="?android:attr/dividerVertical"

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

代码语言:javascript
复制
<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>
票数 165
EN

Stack Overflow用户

发布于 2012-08-19 03:54:51

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

代码语言:javascript
复制
    style="?android:attr/borderlessButtonStyle"

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

然后,您可以像在Karl's answer中那样添加分隔符。

票数 50
EN

Stack Overflow用户

发布于 2013-02-02 23:00:28

回答晚了,但有很多观点。由于API< 11还没有消亡,对于那些感兴趣的人来说,这里是一个诀窍。

让你的容器有想要的颜色(可能是透明的)。然后给你的按钮一个默认透明颜色的选择器,并在按下时设置一些颜色。这样你就会有一个透明的按钮,但当按下时会改变颜色(就像全息图一样)。您还可以添加一些动画(如全息)。选择器应该是这样的:

代码语言:javascript
复制
res/drawable/selector_transparent_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
          android:exitFadeDuration="@android:integer/config_shortAnimTime">
     <item android:state_pressed="true"
         android:drawable="@color/blue" />

   <item android:drawable="@color/transparent" />
</selector>

并且按钮应该有android:background="@drawable/selector_transparent_button"

PS:让你的容器有分隔符( API <11的android:divider='@android:drawable/...)

PS新手:您应该在values/colors.xml中定义这些颜色

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8855791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档