Android中button按钮怎么设置圆角

来自:网络
时间:2023-07-25
阅读:

在Android中,可以通过创建一个自定义的Drawable XML文件来实现给Button设置圆角的效果。

以下是创建圆角按钮的步骤:

1.在 res/drawable 文件夹下,创建一个新的XML文件,例如 rounded_button.xml。

2.在 rounded_button.xml 文件中,添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="8dp" />
            <solid android:color="@color/your_button_color" />
            <stroke android:width="2dp" android:color="@color/your_stroke_color" />
        </shape>
    </item>
</selector>

3.在这里,你可以自定义圆角的半径、按钮的颜色和边框颜色。例如,可以将 android:radius 的值设置为你需要的圆角半径,将 android:color 的值设置为你需要的按钮颜色。

4.接下来,将 rounded_button.xml 设置为 Button 的背景。在你的布局XML文件中,找到你想设置圆角的Button,然后添加 android:background 属性,如下所示:

<Button
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:background="@drawable/rounded_button" />

现在,你的Button应该具有圆角效果了。请注意,你可能需要根据你的需求调整其他Button属性,例如 android:textSize、android:textColor 等。

总结

返回顶部
顶部