您的位置:首页 > 移动开发 > Android开发

android中 关于listitem 获得焦点时,改变其背景

2010-09-09 11:45 531 查看
其实这个问题一直困扰着我,后来终于解决了。其实很简单看下面的截图,不好意思,截图有点模糊:



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"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
	Non focused states 
	<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/list_item_color" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/list_item_sel_color" />
     Focused states
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/list_item_sel_color" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/list_item_sel_color" /> 
    Pressed <item android:state_pressed="true" android:drawable="@drawable/list_item_sel_color" />

</selector>




以上就可以完成背景的设置 效果如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: