



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android ListView 滑动背景为黑色的解决办法在Android中,ListView是最常用的一个控件,在做UI设计的时候,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android:background=drawable/bg,不过不要高兴地太早,当你这么做以后,发现背景是变了,但是当你拖动,或者点击list空白位置的时候发现ListItem都变成黑色的了,破坏了整体效果,如下图所示:这是为什么呢?这个要从Listview的效果说起,默认的ListItem背景是透明的,而ListView的背景是固定不变的,所以在滚动条滚动的过程中如果 实时地去将当前每个Item的显示内容跟背景进行混合运算,所以android系统为了优化这个过程用,就使用了一个叫做 android:cacheColorHint的属性,在黑色主题下默认的颜色值是#191919,所以就出现了刚才的画面,有一半是黑色的.那怎么办呢?如果你只是换背景的颜色的话,可以直接指定android:cacheColorHint为你所要的颜色,如果你是用图片做背景的话,那也只要将 android:cacheColorHint指定为透明(#00000000)就可以了,当然为了美化是要牺牲一些效率的。最后就不回出现上面所说的你 不想要的结果了!自定义ListView行间的分割线在Android平台中系统控件提供了灵活的自定义选项,所有基于ListView或者说AbsListView实现的widget控件均可以通过下面的方法设置行间距的分割线,分割线可以自定义颜色、或图片。在ListView中我们使用属性 android:divider=#FF0000 定义分隔符为红色,当然这里值可以指向一个drawable图片对象,如果使用了图片可能高度大于系统默认的像素,可以自己设置高度比如6个像素 android:dividerHeight=6px ,Android开发网提示当然在Java中ListView也有相关方法可以设置。1)点击Item时无背景颜色变化在xml文件中的ListView控件中加入如下属性:android:listSelector=drawable/timer_list_selector在drawable中定义timer_list_selector的属性值timer_list_selector.xml中定义如下:在values文件夹下的colors.xml中定义transparent如下:#500000002)设置Item之间无间隙在xml文件中ListView控件中加入如下属性:android:divider=#00000000或者在javaCode中如下定义:listView.setDividerHeight(0);3)自定义的BaseAdapter中调用notifyDataSetChanged()方法会重新调用BaseAdapter的getView()方法。用心的朋友应该会发现,listview中在设置了背景之后。会有些问题。1.、listview在拖动的时候背景图片消失变成黑色背景。等到拖动完毕我们自己的背景图片才显示出来。2 、listview的上边和下边有黑色的阴影。3、lsitview的每一项之间需要设置一个图片做为间隔。针对以上问题 在listview的xml文件中设置一下语句。问题1 有如下代码结解决 android:scrollingCache=false问题2 用如下代码解决:android:fadingEdge=none 问题3 用如下代码解决: android:divider=drawable/list_driver 其中 drawable/list_driver 是一个图片资源总体如下Why is my list black? An Android optimization Posted by Romain Guy on 13 January 2009 at 11:20 AM ListView is one of Androids most widely used widgets. It is rather easy to use, very flexible and incredibly powerful. ListView can also be difficult to understand at times.One of the most common issues with ListView happens when you try to use a custom background. By default, like many Android widgets, ListView has a transparent background which means yo can see through the default windows background, a very dark gray (#FF191919 with the current dark theme.) Additionally, ListView enables the fading edges by default, as you can see at the top of the following screenshot; the first text item gradually fades to black. This technique is used throughout the system to indicate that the container can be scrolled.Androids default ListViewThe fade effect is implemented using a combination of Canvas.saveLayerAlpha() and the Porter-Duff Destination Out blending mode. This technique is similar to the one explained in Filthy Rich Clients and various presentations. Unfortunately, things start to get ugly when you try to use a custom background on the ListView or when you change the windows background. The following two screenshots show what happens in an application when you change the windows background. The left image shows what the list looks like by default and the right image shows what the list looks like during a scroll initiated with a touch gesture:Dark fade Dark listThis rendering issue is caused by an optimization of the Android framework enabled by default on all instances of ListView (for some reason, I forgot to enable it by default on GridView.) I mentioned earlier that the fade effect is implemented using a Porter-Duff blending mode. This implementation works really well but is unfortunately very costly and can bring down drawing performance by quite a bit as it requires to capture a portion of the rendering in an offscreen bitmap and then requires extra blending (which implies readbacks from memory.)Since ListView is most of the time displayed on a solid background, there is no reason to go down that expensive route. Thats why we introduced an optimization called the cache color hint. The cache color hint is an RGB color set by default to the windows background color, that is #191919 in Androids dark theme. When this hint is set, ListView (actually, its base class View) knows it will draw on a solid background and therefore replaces th expensive saveLayerAlpha()/Porter-Duff rendering with a simple gradient. This gradient goes from fully transparent to the cache color hint value and this is exactly what you see on the image above, with the dark gradient at the bottom of the list. However, this still does not explain why the entire list turns black during a scroll.As I said before, ListView has a transparent/translucent background by default, and so all default Android widgets. This implies that when ListView redraws its children, it has to blend the children with the windows background. Once again, this requires costly readbacks from memory that are particularly painful during a scroll or a fling when drawing happens dozen of times per second. To improve drawing performance during scrolling operations, the Android framework reuses the cache color hint. When this hint is set, the framework copies each child of the list in a Bitmap filled with the hint value (this assumes that another optimization, called scrolling cache, is not turned off.) ListView then blits these bitmaps directly on screen and because these bitmaps are known to be opaque, no blending is required. And since the default cache color hint is #191919, you get a dark background behind each item during a scroll.To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. This can be dome from code
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年软件行业软件企业法务专业考试试题集及答案详解
- 2025年初入股市投资者必-备知识手册及模拟题集
- 2025年教育行业教师招聘考试备考指南与技巧
- 2025年物业服务质量管理高级考试试题集萃
- XX市天地车人综合监管系统项目建设用户需求书
- 2025年低碳技术与碳排放管理模拟考试题库
- 2025年注册会计师考试财务成本管理备考攻略
- 2025年新媒体运营主管的面试模拟题和答案宝典
- 2025年物流仓储管理面试实战指南与预测题
- 2025年计算机等级考试考前冲刺模拟题及解析文档
- 2025年安全生产法律法规培训
- 2025年现代通信技术基础与应用考试卷及答案
- 2025年互联网数据中心数据中心安全风险评估报告
- 工业互联网与石化化工行业融合应用参考指南(2025年)
- CJ/T 189-2007钢丝网骨架塑料(聚乙烯)复合管材及管件
- 矿石运输居间合同范本
- 乡村旅游与休闲农业融合发展模式创新与实践案例研究报告
- 2025年餐饮服务从业人员食品安全知识培训考试题及答案
- 2025年湖北省公务员录用考试《行测》真题及答案解析(记忆版)
- 安宁疗护之症状管理
- 静脉输血操作规范与风险防控
评论
0/150
提交评论