Android中使用XML自定義組件各種狀態下的背景圖片

需要用到Android的Selector,它可以根據組件的狀態顯示該狀態對應的圖片做爲顯示背景。
把下面的XML文件保存成自己命名的.xml文件,比如list_bg.xml。
注意,這個文件相當於一個背景圖片選擇器,在系統使用時根據ListView中的列表項的狀態來使用相應的背景圖片。
什麼情況使用什麼圖片我在下面都進行了說明。
還有,你可以把它看成是一個圖片來使用,放於drawable目錄下,

配置背景屬性android:background="@drawable/list_bg"就能達到你需要的目的了。


    <?xml version="1.0" encoding="utf-8" ?>   
    <selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_window_focused="false"   
    android:drawable="@drawable/沒有焦點時的圖片背景" />   
    <item android:state_focused="true" android:state_pressed="true"   
    android:drawable=  
    "@drawable/非觸摸模式下獲得焦點並單擊時的背景圖片" />   
    <item android:state_focused="false" android:state_pressed="true"   
    android:drawable="@drawable/觸摸模式下單擊時的背景圖片" />   
    <item android:state_selected="true"   
    android:drawable="@drawable/選中時的圖片背景" />   
    <item android:state_focused="true"   
    android:drawable="@drawable/獲得焦點時的圖片背景" />   
    </selector>   


源代碼ListView列表項背景的默認實現
SDK目錄/tools/lib/res/default/drawable/list_selector_background.xml
內容如下:
    <?xml version="1.0" encoding="utf-8"?>  
    <!-- Copyright (C) 2008 The Android Open Source Project  
      
         Licensed under the Apache License, Version 2.0 (the "License");  
         you may not use this file except in compliance with the License.  
         You may obtain a copy of the License at  
        
              http://www.apache.org/licenses/LICENSE-2.0  
        
         Unless required by applicable law or agreed to in writing, software  
         distributed under the License is distributed on an "AS IS" BASIS,  
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
         See the License for the specific language governing permissions and  
         limitations under the License.  
    -->  
      
    <selector xmlns:android="http://schemas.android.com/apk/res/android">  
      
        <item android:state_window_focused="false"  
            android:drawable="@color/transparent" />  
      
        <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->  
        <item android:state_focused="true" android:state_enabled="false"  
            android:state_pressed="true"  
            android:drawable="@drawable/list_selector_background_disabled" />  
        <item android:state_focused="true" android:state_enabled="false"  
            android:drawable="@drawable/list_selector_background_disabled" />  
      
        <item android:state_focused="true" android:state_pressed="true"  
            android:drawable="@drawable/list_selector_background_transition" />  
        <item android:state_focused="false" android:state_pressed="true"  
            android:drawable="@drawable/list_selector_background_transition" />  
      
        <item android:state_focused="true"  
            android:drawable="@drawable/list_selector_background_focus" />  
      
    </selector>  


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