Android ListView的item背景色设置以及item点击无响应等相关问题
Android ListView的item背景色设置以及item点击无响应等相关问题
在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的item默认的外观,下面讲解以下在使用listview时最常见的几个问题。
1.如何改变item的背景色和按下颜色
listview默认情况下,item的背景色是黑色,在用户点击时是黄色的。如果需要修改为自定义的背景颜色,一般情况下有三种方法:
1)设置listSelector
2)在布局文件中设置item的background
3)在adapter的getview中设置
这三种方法都能达到改变item默认的背景色和按下颜色,下面来分别讲解,但是在这之前需要先写好selector.xml文件;
- <?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/light_blue"></item>
- <item android:state_pressed="false" android:drawable="@color/sgray"></item>
- </selector>
在改变button或者listview的item默认背景色,就可以用到selector。drawable可以设置为色彩资源,也可以设置为图片资源。
1)设置listview的listSelector
- <ListView
- android:id="@+id/history_list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:divider="#565C5D"
- android:dividerHeight="3dp"
- android:listSelector="@drawable/selector"
- android:cacheColorHint="@android:color/transparent">
- </ListView>
2)在listitem的布局文件中设置background属性,下面是listitem的布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/selector">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="历史记录"
- android:textColor="#ffffff"
- android:textSize="20sp"
- android:layout_centerInParent="true">
- </TextView>
- </RelativeLayout>
3)在adapter的getView方法中设置
- if(convertView ==null)
- {
- convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);
- }
- convertView.setBackgroundResource(R.drawable.selector);
上述方法都能达到同样的效果,就是改变item默认的背景色和点击时的背景颜色,第三种方法最灵活,如果listview的奇数行和偶数行需要设置为不同的selector,只能用第三种方法。
2.包含button,checkbox等控件时点击无响应问题。
如果listitem里面包括button或者checkbox等控件,默认情况下listitem会失去焦点,导致无法响应item的事件,最常用的解决办法是在listitem的布局文件中设置descendantFocusability属性。
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="10dp"
- android:paddingBottom="10dp"
- android:paddingLeft="5dp"
- android:paddingRight="5dp"
- android:descendantFocusability="blocksDescendants">
- <CheckBox
- android:id="@+id/history_item_checkbt"
- android:layout_height="30dp"
- android:layout_width="wrap_content"
- android:layout_centerVertical="true"
- android:layout_alignParentLeft="true"
- android:checked="false"
- >
- </CheckBox>
- <ImageView
- android:id="@+id/history_item_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_toRightOf="@id/history_item_checkbt"
- android:background="@drawable/item_icon">
- </ImageView>
- <Button
- android:id="@+id/history_item_edit_bt"
- android:layout_alignParentRight="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:text="编辑"
- android:textColor="#ffffff"
- android:textSize="14sp"
- android:background="@drawable/button_bg">
- </Button>
- <TextView
- android:id="@+id/history_item_time_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:textColor="#565C5D"
- android:textSize="14sp"
- android:text="10-01 10:20"
- android:layout_marginRight="5dp"
- android:layout_toLeftOf="@id/history_item_edit_bt">
- </TextView>
- <TextView
- android:id="@+id/history_item_title_tv"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_centerVertical="true"
- android:textColor="#565C5D"
- android:textSize="14sp"
- android:text="xxxxxxxxXXXXXXXXXXXXXXXX"
- android:ellipsize="end"
- android:maxLines="1"
- android:layout_toRightOf="@id/history_item_image"
- android:layout_toLeftOf="@id/history_item_time_tv"
- android:layout_marginLeft="3dp">
- </TextView>
- </RelativeLayout>
Android ListView的item背景色设置以及item点击无响应等相关问题的更多相关文章
- android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
android ListView的上部下拉刷新下部点击加载更多具体实现及拓展 ListView下拉刷新,上拉自动加载更多 下拉刷新以及加载更多
- android stuido 在线安装svn插件,添加版本库无响应
问题:android stuido 中在线安装svn插件,添加版本库无响应. 原因: 由于android stuido 版本较高,在线安装1.6x 版本的svn,添加版本库一直没有响应,最后卡死.. ...
- 解决httpclient设置代理ip之后请求无响应的问题
httpclient这个工具类对于大家来说应该都不陌生吧,最近在使用过程中出现了碰到一个棘手的问题,当请求的接口地址由http变成https之后,程序执行到 httpClient.execute(ht ...
- Android ListView的item背景色设置
1.如何改变item的背景色和按下颜色 listview默认情况下,item的背景色是黑色,在用户点击时是黄色的.如果需要修改为自定义的背景颜色,一般情况下有三种方法: 1)设置listSelecto ...
- Android ListView的item点击无响应的解决方法
假设listitem里面包含button或者checkbox等控件,默认情况下listitem会失去焦点,导致无法响应item的事件,最经常使用的解决的方法 是在listitem的布局文件里设置des ...
- Android ListView嵌套Button,Button事件覆盖item事件解决办法
方法就是修改item布局的xml文件: 在根布局里加上: android:descendantFocusability="blocksDescendants" 然后在按钮布局里加上 ...
- [Android]ListView中分割线的设置
1.在布局文件中ListView元素中通过属性设置 android:divider="#fffff" 分割线颜色 android:dividerHeight="1px& ...
- Android ListView中按钮监听器设置的解决方案
在做安卓应用开发的时候很经常会用到ListView,并且每一个Item里面都会有按钮之类的需要进行事件监听的控件.在给按钮添加OnClickListener的时候,一开始很下意识的会想在ListVie ...
- Android ListView的header footer设置visibility gone不起作用
常用的ViewGroup,例如LinearLayout,在onMeasure方法内对每个child view执行measure前,会判断child view的visibility是否为gone.如果是 ...
随机推荐
- List,泛型和Datatable 的相互转换
public static DataTable ToDataTableTow(IList list) { DataTable result = new DataTable(); ) { Propert ...
- kindeditor扩展粘贴截图功能&修改图片上传路径并通过webapi上传图片到图片服务器
前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...
- linux 根据端口查看系统进程
1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 注意不同用户下,查看的进程不同
- linux overcommit flag
linux中有一个overcomit的配置,这个配置关系到进程在过多申请memory资源的时候,系统的表现(启发式允许,不检查,or 阻止) /proc/sys/vm/overcommit_memor ...
- npm无响应处理办法
方法一:使用cnpm 1. 安装 `npm install cnpm -g` 2.设置使用淘宝镜像源 `npm install cnpm -g --registry=https://registry. ...
- Python快速学习02:基本数据类型 & 序列
前言 系列文章:[传送门] 也就每点一点点的开始咯,“还有两年时间,两年可以学很多东西的” Python ['paɪθən] n. 巨蛇,大蟒 基本数据类型 变量不需要声明 a=10 # int 整 ...
- Net使用RdKafka引发异常RdKafka.Internal.LibRdKafka 的类型初始值设定项引发异常
在Net中VS2015用RdKafka组件开发消息发布和消费,引发下面的异常 RdKafka.Internal.LibRdKafka 的类型初始值设定项引发异常System.TypeInitializ ...
- 【原创】驱动加载之OpenService
SC_HANDLE WINAPI OpenService( _In_ SC_HANDLE hSCManager, _In_ LPCTSTR lpServiceName, _In_ DWORD dwDe ...
- TensorFlow和深度学习-无需博士学位(TensorFlow and deep learning without a PhD)
1. 概述 原文地址: TensorFlow and deep learning,without a PhD Learn TensorFlow and deep learning, without a ...
- SQL Server 2008 R2 下如何清理数据库日志文件
废话不多说,直接上代码,清理后日志文件为1M USE [master] GO ALTER DATABASE [数据库名] SET RECOVERY SIMPLE WITH NO_WAIT GO ALT ...