android中 關於listitem 獲得焦點時,改變其背景

來源:互聯網
上載者:User

  其實這個問題一直困擾著我,後來終於解決了。其實很簡單看下面的,不好意思,有點模糊:

1、listView控制項
       <ListView
  android:id="@+id/contact_listview"
android:layout_width="wrap_content"
android:layout_height="400dip"
  android:divider="#b9b9b9"
  android:dividerHeight="1px"
  android:cacheColorHint="#00000000"
  android:listSelector="@drawable/list_selector"
  android:headerDividersEnabled="true">
</ListView>

如果是繼承ListActivity的話,則可以這樣設定。

this.getListView().setSelector(getResources().getDrawable(R.drawable.list_selector));

 

2、在drawable檔案夾中見一個list_selector.xml檔案,如果什麼內容都不定,只是用來把系統內建的給覆蓋掉,在這裡我們添加上我要添加的小圖片,其中list_item_color.png只是一張只有10*10透明的圖片,list_item_sel_color也是一 張10*10的淺藍色的圖片。 

list_item_sel_color:    

list_item_color:    
<?xml version="1.0" encoding="UTF-8"?><br /><selector xmlns:android="http://schemas.android.com/apk/res/android"><br />Non focused states<br /><item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/list_item_color" /><br /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/list_item_sel_color" /><br /> Focused states<br /> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/list_item_sel_color" /><br /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/list_item_sel_color" /><br /> Pressed <item android:state_pressed="true" android:drawable="@drawable/list_item_sel_color" /></p><p></selector>

 

以上就可以完成背景的設定 效果如下:

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.