Android 自定义按钮半径(Button)
今天遇到了一个问题,问题是如何改变Button的样式。原来的样式不好看,我想要的是圆形的和其他形式的怎么办呢?我搜了一些资料,资料比较多我把简单的代码示例分享给大家。
button_bg.xml 文件应该放在 drawable 目录下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按钮半径 -->
<corners
android:bottomLeftRadius="20dp"
android:topRightRadius="20dp"
android:topLeftRadius="20dp"
android:bottomRightRadius="20dp"/>
<!-- 背景颜色 -->
<solid android:color="@color/colorPrimary"/>
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="我是按钮"
android:background="@drawable/button_bg"
android:textSize="22sp"
android:textColor="#fff"
android:padding="40dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
效果如下: