1. 程式人生 > >自定義圓形按鈕並實現按下狀態

自定義圓形按鈕並實現按下狀態

首先在drawable 檔案下新建兩個 xml檔案

button_ellipse_tint_green.xml //淺綠色的圓角

button_ellipse_green.xml  綠色的圓角

兩個xml程式碼都一樣,下面是程式碼,自行修改填充顏色。填充顏色就是不同的按下顏色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 圓角buttton -->

<!--查詢合格產品使用這個按鈕-->
    <!-- 填充顏色 -->
    <solid android:color="#33A357" />
    <!-- 設定按鈕的四個角為弧形 -->
    <corners android:radius="80dp" />

    <!-- padding:button裡面的文字與button邊界的間隔 -->
    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp" />

</shape>

以上操作完成後,在新建一個 button_ellipse_press.xml

把剛剛建立的xml 新增進來。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--沒有按下 狀態:綠色-->
    <item android:drawable="@drawable/button_ellipse_green"  android:state_pressed="false"></item>
    
    <!--淺綠色-->
    <item android:drawable="@drawable/button_ellipse_tint_green" android:state_pressed="true"></item>
 
    
   <!--   <item android:drawable="@drawable/button_ellipse" />-->

</selector>

最後去你要設定的按鈕下,設定      android:background="@drawable/button_ellipse_press"