android设置button的形状
时间: 2023-10-31 09:05:21 浏览: 59
在Android中,可以通过设置Button的背景来改变其形状。一种常见的做法是使用XML文件定义Button的背景,以实现圆角、椭圆等形状。
以下是一个示例,实现了一个圆形Button:
1. 在res/drawable目录下创建一个XML文件,命名为round_button.xml。
2. 在round_button.xml中定义Button的背景。
```
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorPrimary" />
</shape>
```
上述代码中,我们使用shape元素定义了一个形状为圆形的背景,使用solid元素填充了背景颜色。
3. 在布局文件中设置Button的背景为round_button.xml。
```
<Button
android:id="@+id/btn_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_button"
android:text="Round Button" />
```
这样,我们就可以将Button的形状设置为圆形。类似地,我们也可以通过定义其他形状的XML文件,来改变Button的形状。