自定义圆形按钮 [英] Custom circle button

查看:43
本文介绍了自定义圆形按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自定义按钮,我需要它是圆形的.如何创建圆形按钮?我认为 draw9patch 是不可能的.

I want to create custom button and I need it to be circle. How can I create a circle button? I do not think that be possible with draw9patch.

我也不知道如何制作自定义按钮!

Also I do not know how to make custom button!

你有什么建议吗?

推荐答案

像这样使用 xml drawable:

Use xml drawable like this:

将以下内容保存为round_button.xmldrawable文件夹

Save the following contents as round_button.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="oval">
            <solid android:color="#fa09ad"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="#c20586"/>
        </shape>
    </item>
</selector>

Android 材质效果: 虽然 FloatingActionButton 是一个更好的选择,如果你想使用 xml 选择器来做,在 res<中创建一个文件夹 drawable-v21/code> 并使用以下 xml 保存另一个 round_button.xml

Android Material Effect: Although FloatingActionButton is a better option, If you want to do it using xml selector, create a folder drawable-v21 in res and save another round_button.xml there with following xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#c20586">
    <item>
        <shape android:shape="oval">
            <solid android:color="#fa09ad"/>
        </shape>
    </item>
</ripple>

并将其设置为 xml 中 Button 的背景,如下所示:

And set it as background of Button in xml like this:

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="hello"
android:textColor="#fff" />

重要提示:

  1. 如果您希望它显示所有这些状态(启用、禁用、突出显示等),您将使用选择器,如此处所述.
  2. 您必须保留这两个文件,以使 drawable 向后兼容.否则,您将在以前的 android 版本中遇到奇怪的异常.

这篇关于自定义圆形按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆