设置Button背景渐变效果和点击效果

来源:互联网 发布:打字慢能做淘宝客服吗 编辑:程序博客网 时间:2024/05/16 15:05

1、设置背景渐变效果,在drawable目录下建buttonshape.xml文件,

内容为:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient android:startColor="#01DFD7" android:endColor="#81F7F3" 
            android:angle="270"/> 
    <corners 
         android:bottomRightRadius="10dip" 
         android:bottomLeftRadius="1dip" 
         android:topLeftRadius="18dip" 
         android:topRightRadius="10dip"/> 
</shape>

这里startColor是开始颜色,endColor是渐变结束颜色,默认是从上往下渐变,可以使用android:centerY调节,android:angle="270"设置角度。

corners设置边角的圆滑度。

设置点中的效果,在drawable目录下建buttonshape_down.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient android:startColor="#F5F510" android:endColor="#F5F5B0" 
        android:angle="270" /> 
    <corners android:bottomRightRadius="10dip" 
        android:bottomLeftRadius="1dip" android:topLeftRadius="18dip" 
        android:topRightRadius="10dip" /> 
</shape>

在res目录下新建文件夹xml,然后在xml目录下,新建selectshape.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
        android:state_pressed="false" 
        android:drawable="@drawable/buttonshape" /> 
    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/buttonshape_down" /> 
    <item 
        android:drawable="@drawable/buttonshape" android:state_window_focused="false"/> 
</selector>

state_pressed设置按钮状态,在main.xml文件中设置Button的属性:

<Button android:id="@+id/button_reimbursementrecords" 
            android:text="报销记录" android:layout_width="280px" 
            android:layout_height="80px" android:textSize="30px" 
            android:background="@xml/selcetshape" />

使用background设置按钮的背景色。