設置Button背景漸變效果和點擊效果

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設置按鈕的背景色。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章