通過xml佈局文件實現按鈕改變焦點設置背景圖片

Button的android:background屬性

    設置按鈕背景圖片:
        onFocus()與onClick()事件的處理
        Item的android:state_focused和android:state_pressed屬性的實現

    在res文件夾下建立文件夾drawable,在drawable文件下穿件styles.xml文件,文件內容如下:




<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 失去焦點 -->
    <item android:drawable="@drawable/png_3" android:state_focused="false" android:state_pressed="true"/>
    <!-- 默認時候圖片 -->
    <item android:drawable="@drawable/png_4"></item>

</selector>
    

在main.xml文件中創建組件ImageButton時候如下



    <ImageButton
        android:id="@+id/image_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:background="@drawable/styles"/>


這樣,在使用ImageButton時候未點擊圖片按鈕時候,圖片顯示的是png4   而點擊之後顯示的是png3圖片,放開點擊有顯示默認的圖片
發佈了32 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章