android listview selector
来源:互联网 发布:知我药妆那里来的小样 编辑:程序博客网 时间:2024/05/17 22:49
http://www.cnblogs.com/tt_mc/archive/2012/05/30/2526599.html
The solution by dglmtn doesn't work when you have a 9-patch drawable with padding as background. Strange things happen, I don't even want to talk about it, if you have such a problem, you know them.
Now, If you want to have a listview with different states and 9-patch drawables (it would work with any drawables and colors, I think) you have to do 2 things:
- Set the selector for the items in the list.
- Get rid of the default selector for the list.
What you should do is first set the row_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@drawable/list_item_bg_pressed" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@drawable/list_item_bg_focused" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="@drawable/list_item_bg_focused" />
<item
android:drawable="@drawable/list_item_bg_normal" />
</selector>
Don't forget the android:state_selected
. It works like android:state_focused
for the list, but it's applied for the list item.
Now apply the selector to the items (row.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/row_selector"
>
...
</RelativeLayout>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@color/android:transparent" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@color/android:transparent" />
<item
android:drawable="@color/android:transparent" />
</selector>
Notice that it makes all the states transparent. That's what I meant by getting rid of the default states.
Last, but not least - apply the selector to the listview:
android:id="@+id/android:list"
...
android:listSelector="@drawable/list_selector"
/>
- android listview selector
- android listview selector
- android listview selector
- android listview 单选多选 selector
- android ListView Selector背景选择器
- android listview 设置点击效果selector
- Android中ListView的Selector的用法
- Android中ListView中selector使用
- 浅谈android的selector背景选择器(ListView/GridView)
- 浅谈android的selector背景选择器(ListView/GridView) .
- android selector(改变button,textview,listview的背景图片)
- 浅谈android的selector背景选择器(ListView/GridView)
- 浅谈android的selector背景选择器(ListView/GridView)
- listview美化之selector
- listview和button -- selector
- ListView设置selector无效
- android selector
- android selector
- hibernate学习笔记18
- shell语法简介
- 类型转换函数(特此记录)
- java方面中,参数后面跟三个点(...)的含义
- [2月26日更新]OpenWRT for Buffalo WHR-G300Nv2/G301N
- android listview selector
- C++ STL hash_map
- 反汇编算法介绍和应用——线性扫描算法分析
- Mantis邮件配置和Bug基本流程
- 古筝教学视频——袁莎周望李萌林玲
- 【Win XP】查看XP系统开机和关机时间
- 爬虫技术
- 转的不错的makefile模板
- 搭建VLC流媒体服务