很多时候会用到圆角button,那么如何使用xml来绘制圆角按钮呢?
1 在res/drawable文件夹下新建btn_bg_black_corner.xml文件:
<?xml version="1.0" encoding="UTF-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle" >  
    <!-- 填充的颜色 -->  
    <solid android:color="#ff000000" />  
    <!-- 设置矩形的四个角为弧形 -->  
    <!-- android:radius 弧形的半径 -->  
    <corners android:radius="7dip" />  
</shape> 
2 在res/drawable文件夹下新建btn_bg_black_corner.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
</shape>  
3 在res/drawable文件夹下新建bt_back.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/btn_bg_black_corner"></item>
    <item android:drawable="@drawable/btn_bg_white_corner"></item>
</selector>  
4 在main_activty.xml中作为Button的背景显示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.wxyass.cornersbutton.MainActivity" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bt_back"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="这是一个圆角按钮"
        android:textSize="18sp" />
</RelativeLayout>  
5 运行程序,圆角按钮成功显示.
