自定义圆圈键 [英] Custom circle button

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

问题描述

我想创建自定义按钮,我需要它是圆。如何创建一个圆形按钮? 我不认为这是可能的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绘制这样的:

Use xml drawable like this:

将下面的内容 round_button.xml 绘制文件夹

<?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的重大影响:虽然<一href="https://developer.android.com/intl/ko/reference/android/support/design/widget/FloatingActionButton.html"><$c$c>FloatingActionButton是一个更好的选择,如果你想这样做,使用XML选择,创建资源文件夹绘制-V21 和拯救他人 round_button.xml 有与下面的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的背景是这样的:

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. 您已经保留,以使绘制向后兼容这两个文件。否则,你要面对怪异的例外previous Android版本。
  1. If you want it to show all these states (enabled, disabled, highlighted etc), you will use selector as described here.
  2. You've to keep both files in order to make the drawable backward-compatible. Otherwise, you'll face weird exceptions in previous android version.

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

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