Android开发之ListView选中某行后改变其背景色
来源:互联网 发布:防水运动鞋 知乎 编辑:程序博客网 时间:2024/05/16 08:19
Android开发之ListView选中背景色
一、在ListView中手指按下选中某一行时,更换选中那一行的背景色,
放开之后恢复原来的背景色
首先在项目的res路径下的drawable文件中创建一个xml文件(如果没有drawable文件自己新建一个),然后在此xml文件中加入如下代码:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@color/colorlvBtn"></item> <item android:state_pressed="true" android:drawable="@color/colorlvBtn"/></selector>
这两句代码的意思就是:当ListView中的某一行为焦点时,以及你触碰某一行时,改变此行的背景色。
当然,其中的@color后面的代码也是个xml文件,需要在其他地方定义,我是在drawable同级目录下创建了一个叫values的文件夹,然后在此文件夹中创建了colors.xml的文件,代码如下:
<?xml version="1.0" encoding="utf-8"?><resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="colorlvBtn">#FF9900</color> <color name="colorlvBtnBehind">#0000</color></resources>
这样的好处是方便后期管理。
最后,在你需要改变背景色的ListView中加入:
android:listSelector="@drawable/listview_button"
这个属性(listview_button是你最开始创建的xml文件),然后你就会发现你触摸某一行之后背景色就会改变,放开时背景色又变回来了。
二、按下ListView的某一行之后,改变其背景色,但不恢复到原来的背景色。
要实现这个效果也很简单,你只需在listview的点击事件中:
某listview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1,int index, long arg3) { 某listview.setSelector(R.color.colorlvBtn); }}
即上面列出的那个事件中加入如下代码:
//设置选中行的背景色某listview.setSelector(R.color.colorlvBtn);
当然,colorlvBtn也是个xml文件,具体创建方法上面已列出。然后,在你点击某一行后,那一行的背景色就会发生改变,且不会再变回原来的背景色。
阅读全文
0 0
- Android开发之ListView选中某行后改变其背景色
- ListView单击、选中、时改变背景色
- android 设置listview item选中背景色
- android 设置listview item选中背景色
- ListView中的Item被选中怎么改变背景色?
- Android开发入门例程之动态改变背景色
- GridView选中改变背景色
- Android开发教程:xml文件中设置listview item选中背景色
- android listview item 被选中背景色设置
- Android美工坊--listview更改选中时item背景色
- Android美工坊--listview更改选中时item背景色
- Android美工坊--listview更改选中时item背景色
- Android美工坊--listview更改选中时item背景色
- Android listview更改选中时item背景色
- Android UI--listview更改选中时item背景色
- Android ListView选中列表项修改背景色
- Android ListView 去除边缘阴影、选中色、拖动背景色
- android listview设置选中时的item的背景色
- android开发多国语言对照表
- 用cocoapods导入的三方库方法无法识别问题
- DELPHI高性能大容量SOCKET并发(四):粘包、分包、解包
- python_csv模块学习
- 北漂的日常
- Android开发之ListView选中某行后改变其背景色
- FTP常用命令以及客户端介绍
- ES6解构赋值
- Luogu 1186(SPFA)
- Oracle删除表空间tablespace
- js控件的操作,以后学到的所有js控件都放到这里
- ubuntu 下编译android libjpeg-turbo-1.5.2
- POJ 3304:Segments 计算几何 是否有直线与所有线段相交
- 深入剖析,自己实现Butter knife