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

一、設置背景漸變效果,在drawable目錄下建buttonshape.xml文件,android

內容爲:web

<?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>spa

這裏startColor是開始顏色,endColor是漸變結束顏色,默認是從上往下漸變,能夠使用android:centerY調節,android:angle="270"設置角度。orm

corners設置邊角的圓滑度。xml

設置點中的效果,在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>ip

在res目錄下新建文件夾xml,而後在xml目錄下,新建selectshape.xml文件,內容以下:utf-8

<?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>get

設置了圖片的觸發狀態,你能夠設置 state_pressed,state_checked,state_pressed,state_selected,state_focused,state_enabled 等幾個狀態
it

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

相關文章
相關標籤/搜索