我想创建自定义按钮,并且需要它是圆形的。如何创建圆形按钮?
我认为使用draw9patch 不可能做到这一点。
我也不知道如何制作自定义按钮!
您有什么建议吗?
像这样使用 xml 绘图:
将以下内容另存为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>
安卓材质效果:虽然FloatingActionButton https://developer.android.com/intl/ko/reference/android/support/design/widget/FloatingActionButton.html是一个更好的选择,如果您想使用 xml 选择器来执行此操作,请创建一个文件夹drawable-v21
in res
并保存另一个round_button.xml
有以下 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>
并将其设置为背景Button
在 xml 中是这样的:
<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" />
重要的:
- 如果您希望它显示所有这些状态(启用、禁用、突出显示等),您将使用选择器作为此处描述 https://stackoverflow.com/a/1726352/593709.
- 您必须保留这两个文件才能使可绘制对象向后兼容。否则,你会在以前的 Android 版本中遇到奇怪的异常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)