Android開發之按鈕點擊效果淺析

在Android開發中, 扁平化按鈕已常見不鮮, 得空也琢磨了一下,也並不複雜,應付日常開發,這些也夠用了.

首先,在drawable中建兩個.xml文件,normal.xmlpressed.xml

常見的Attributes字段:

android:state_pressed Boolean “true”表示按下狀態使用;“false” 表示非按下狀態使用。 
android:state_focused  Boolean “true”表示聚焦狀態使用(例如使用滾動球/D-pad聚焦Button);“false”表示非聚焦狀態使用。 
android:state_selected  Boolean “true”表示選中狀態使用(例如Tab 打開);“false”表示非選中狀態使用。 
android:state_checkable Boolean “true”表示可勾選狀態時使用;“false”表示非可勾選狀態使用。(只對能切換可勾選—非可勾選的構件有用。) 
android:state_checked Boolean “true”表示勾選狀態使用;“false”表示非勾選狀態使用。 
android:state_enabled Boolean “true”表示可用狀態使用(能接收觸摸/點擊事件);“false”表示不可用狀態使用。 
android:window_focused Boolean “true”表示應用程序窗口有焦點時使用(應用程序在前臺);“false”表示無焦點時使用(例如Notification欄拉下或對話框)

在xml文件中 layer-list下shape有幾個主要的 Attributes字段,需注意:

(1)solid:填充顏色

(2)corners:圓角幅度

(3)stroke:設置周邊 width:邊框寬度 color:邊框顏色

(4)gradient:設置漸變色

(5)padding:內邊距


bg01_normal:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <corners android:radius="8dp" />
            <solid android:color="@color/burlywood" />
        </shape>
    </item>
    <item android:bottom="4dp">
        <shape android:shape="rectangle" >
            <gradient android:startColor="#FF2f4f4f" android:centerColor="#AA2f4f4f"
                android:endColor="#882f4f4f" />
            <corners android:radius="8dp" />
            
        </shape>
    </item>
</layer-list>
bg01_pressed:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <corners android:radius="8dp" />
            <solid android:color="#00000000" />
        </shape>
    </item>
    <item android:top="4dp">
        <shape android:shape="rectangle">
            <corners android:radius="8dp"/>
            <gradient android:startColor="#FF2f4f4f" android:centerColor="#AA2f4f4f"
                android:endColor="#882f4f4f" />
        </shape>
    </item>
</layer-list>
bg01_selector:

<?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/button_bg01_pressed"></item>
    <item android:drawable="@drawable/button_bg01_normal"></item>

</selector>

最後設置按鈕的background即可:


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