listview更改选中时item背景色(转)
默认情况下使用ListView背景色是黑色,选中item的高亮颜色是菊黄色,很多时候不得不自己定义背景色或者背景图
android:cacheColorHint="@android:color/transparent",意思为去黑色底色,比如ListView滚动时会刷新界面,默认颜色还是系统颜色,所以采用这种方式设置其为透明即可,这个属性在ListView中使用圆角图片来设置ListView时很有用
android:divider="@null"用于去掉listview的item之间的黑线
1、背景色
即在list_item_color_bg.xml中通过设置color来实现点击item时不同的颜色,但是如果使用color的 话,listview无法使用android:listSelector属性,如果设置android:listSelector方式的话,点击一个 item后整体的ListView全部都会变成一种颜色,这时必须采用在item中设置android:background的方式才可以。 android:listSelector方式适用于图片的方式,即类似与(android:drawable="@drawable/img")
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/green"></item>
<item android:drawable="@color/white"></item>
</selector>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
<color name="green">#00ff00</color>
</resources>
下面再看看布局文件
listview.xml,用color的方式,这里不能使用listSelector

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:cacheColorHint="@android:color/transparent"
android:divider="@null"
/>
</LinearLayout>

list_item_color.xml,通过color设置直接在item的布局中设置背景即可

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/list_item_color_bg">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>

效果图

2、背景图
这种方式是在selector文件中采用图片来设置item的背景,无论是设置ListView的android:listSelector的方式 还是设置item的android:background的方式都可以使用,不过最好还是使用android:background的方式,因为使用 android:listSelector的方式时下面的selector文件中设置的默认时的图片
(<item android:drawable="@drawable/login_input"/>)不会显示,而改为background的方式则可以。有些奇怪,希望懂的能指点一下
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/input_over"/>
<item android:drawable="@drawable/login_input"/>
</selector>
listView此时设置如下,这里在item中不设置android:background

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@drawable/list_item_drawable_bg"
/>
</LinearLayout>

此时的效果图如下:背景图是.9.png图片,注意默认的白色.9.png图片login_input没有显示

如果使用android:background的方式,取消android:listSelector的方式,效果如下

listview更改选中时item背景色(转)的更多相关文章
- android中设置ListView的选中的Item的背景颜色
ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的. 1.配置main.xml <? ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
开发中常常需要自己定义Listview,去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点. 如果你的自定义Li ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 改变listview中item选中时文字的颜色
摘要 当listview的某个item选中时,默认有个选中的高亮显示,如果你要自定义选中时的高亮显示效果,可以在listview中设置属性 android:listSelector="@dr ...
- listview当选中某一个item时设置背景色其他的不变
listview当选中某一个item时设置背景色其他的不变: 可以使用listview.setOnFoucsChangeListener(listener) ; /** * listview获得焦点和 ...
- Android ListView的item背景色设置以及item点击无响应等相关问题
Android ListView的item背景色设置以及item点击无响应等相关问题 在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的ite ...
- 53、listview、expandableListview如何选中时保持高亮?
一.listView被选中后保持高亮 70down voteaccepted To hold the color of listview item when you press it, include ...
- wpf datagrid设置右键菜单打开时选中项的背景色
原文:wpf datagrid设置右键菜单打开时选中项的背景色 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/artic ...
- Android ListView之选中(撤销选中)Item
在ContactListActivity中,点击未选中的item将其选中,再点击已选中的item撤销其选中 public void onItemClick(AdapterView<?> p ...
随机推荐
- webpack快速入门——CSS进阶:消除未使用的CSS
使用PurifyCSS可以大大减少CSS冗余 1.安装 cnpm i purifycss-webpack purify-css --save-dev 2.引入glob,因为我们需要同步检查html模板 ...
- vue中axios访问Java后端跨域问题解决
问题背景: 前后端分离,前端选用Vue,后端选用Java,vue编译出的静态页面采用ngix发布,在前端访问后端时出现跨域问题. 解决方法: 跨域的问题解决方法有好多种,这里是通过服务端解决,以下是代 ...
- Docker学习--docker的基本认识
1.Docker 架构 Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器. Docker 容器通过 Docker 镜像来创建. 容器与镜像的关系类似于 ...
- 用asp.net core 2.0 + EFCore.Sqlite做个小网站
许久没用C#写程序.听说进来发生大事,.NetCore2.0发布了,于是便学习了下,本站也应运而生. 大多数的地方按照官方的文档起步走就可以了,这里谈谈遇到的几个坑. 首先,本站是基于ASP.NetC ...
- jQuery中的100个技巧(译)
1.当document文档就绪时执行JavaScript代码. 我们为什么使用jQuery库呢?原因之一就在于我们可以使jQuery代码在各种不同的浏览器和存在bug的浏览器上完美运行. <sc ...
- CentOS命令行界面与图形界面切换(图文详解)
不多说,直接上干货! Ctrl + Alt +F1,到图形界面 Ctrl + Alt +F2,到命令行界面 欢迎大家,加入我的微信公众号:大数据躺过的坑 人工智能躺过的坑 同 ...
- 6.6. Access Control
解读 6.6.2.2. Qualified Access to a protected Constructor https://docs.oracle.com/javase/specs/jls/se7 ...
- 《垃圾回收的算法与实现》——GC标记-清除算法
基本算法 标记-清除算法由 ==标记阶段== 和 ==清除阶段== 构成. 标记即将所有活动的对象打上标记. 清除即将那些没有标记的对象进行回收. 标记与清除 遍历GC root引用,递归标记(设置对 ...
- Maven 学习笔记(一)
什么是 maven? 对于已经使用过 maven 的用户来说 maven 就是一个架构工具,使用它可以将代码构建成一个可发布的工具.当然也有人会说maven是一个项目管理的工具.当然各有各的说法,那么 ...
- android即时通信开源项目信息
源码提供!Android即时通讯和sns开源项目汇总 http://www.jianshu.com/p/b2ca52337fe5