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

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

你有什么建议吗?


当前回答

对于MaterialButton上的FAB样式的按钮:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
    app:cornerRadius="28dp"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:text="1" />

结果:

如果你改变大小,小心使用按钮大小的一半作为app:cornerRadius。

其他回答

AngryTool自定义android按钮

你可以用这个工具网站制作任何类型的自定义android按钮… 我使圆形和方形按钮与圆角与这个工具站点.. 也许我能帮助你

使用xml绘制像这样:

将以下内容保存为drawable文件夹中的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材质效果:虽然FloatingActionButton是一个更好的选择,如果你想使用xml选择器,创建一个文件夹drawable-v21在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>

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

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

重要的是:

如果你想要它显示所有这些状态(启用,禁用,高亮等),你将使用这里描述的选择器。 为了使可绘制文件向后兼容,您必须同时保留两个文件。否则,在以前的android版本中,你会遇到奇怪的异常。

你可以从AndroidX材质库中使用MaterialButton

<com.google.android.material.button.MaterialButton
     android:layout_width="75dp"
     android:layout_height="75dp"
     android:layout_margin="10dp"
     android:insetLeft="0dp"
     android:insetTop="0dp"
     android:insetRight="0dp"
     android:insetBottom="0dp"
     app:cornerRadius="50dp"
     app:icon="@drawable/ic_camera"
     app:iconGravity="textStart"
     app:iconPadding="0dp"
     app:iconSize="35dp" />

就像这样

对于MaterialButton上的FAB样式的按钮:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
    app:cornerRadius="28dp"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:text="1" />

结果:

如果你改变大小,小心使用按钮大小的一半作为app:cornerRadius。

不幸的是,使用XML绘图和覆盖背景意味着你必须显式地设置颜色,而不能使用应用程序样式的颜色。

我没有为每个行为硬编码按钮颜色,而是硬编码角半径,这让人感觉不那么粗陋,并保留了所有默认的按钮行为(按下按钮时改变颜色和其他视觉效果),并默认使用应用程序样式的颜色:

将“android:layout_height”和“android:layout_width”设置为相同的值 设置app:cornerRadius为高度/宽度的一半 (实际上,任何大于或等于高度/宽度一半的值都可以工作,所以为了避免每次更新高度/宽度时都必须改变半径,你可以将其设置为一个非常高的值,如1000dp,如果这种行为发生变化,它可能会崩溃。) 将android:insetBottom和android:insetTop设置为0dp,得到一个完美的圆

例如:

<Button
    android:insetBottom="0dp"
    android:insetTop="0dp"
    android:layout_height="150dp"
    android:layout_width="150dp"
    app:cornerRadius="75dp"
    />