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

    技术2022-05-20  33

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

    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的话,则可以这样设置。

    view plain copy to clipboard print ? 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: 

    view plain copy to clipboard print ? <?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>  

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


    最新回复(0)