转于:http://www.cnblogs.com/loulijun/archive/2012/04/15/2450312.html
-----------------------------------------------------------------------------------------------------
默认情况下使用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的方式,效果如下
相关推荐
当一个item被选中时,我们需要将其背景设为选中颜色,否则设为默认颜色。例如: ```java @Override public View getView(int position, View convertView, ViewGroup parent) { View view = ... // 获取或复用...
《Android应用源码解析:Drag-Sort-Listview深度探讨》 在Android开发中,我们经常需要实现可拖动排序的列表视图,这在诸如购物应用、任务管理器等场景下尤为常见。Drag-Sort-Listview是一个开源库,它为Android...
然而,它在默认情况下并未设置选中项的背景颜色,而是仅提供了一个聚焦效果,即当用户点击ListView的某一项时,该项目会获得一个高亮显示,但一旦手指移开,这个高亮效果就会消失。针对这一需求,我们需要自定义...
Android-react-native-timeline-listview.zip,React本机应用程序的时间线组件,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
本教程将围绕“Android--开发--ListView 中的item随意拖动”这一主题,详细讲解如何实现这个功能。 首先,要实现ListView的拖动功能,我们需要理解ListView的工作原理。ListView通过Adapter来获取并展示数据,每个...
`android-pulltorefresh-listview`是一个专为ListView实现下拉刷新效果的开源库。这个库使得开发者能够轻松地将下拉刷新功能集成到他们的应用中,提高了用户体验。 首先,我们来看看`ListView`。ListView是Android ...
标题与描述均提到了“Android世界中的ListView”,这意味着文章聚焦于Android开发中ListView组件的深入探讨,由Romain Guy和Adam Powell在2010年的一次演讲中提出。ListView是Android应用程序中常用的一种UI控件,...
标题 "pinned-section-listview-master" 指的是一款仿照MIUI7文件管理器中“最近”功能的ListView实现,它特别强调了在ListView头部存在可以固定的分类。这个项目可能是一个开源Android开发示例,旨在帮助开发者创建...
Animated Expanding ListView provides a fancy animation on expanding or collapsing the content of a listview item. It works in all version of Android and it's very easy to adapt to your project. How to...
DragSortListView(DSLV)是Android ListView的一个扩展,支持拖拽排序和左右滑动删除功能。重写了TouchInterceptor(TI)类来提供更加优美的拖拽动画效果。 DSLV主要特性: 完美的拖拽支持; 在拖动时提供更平滑...
《Android下拉刷新ListView详解——基于android-pulltorefresh库》 在移动应用开发中,用户界面的交互性与用户体验至关重要。其中,下拉刷新功能已成为许多Android应用的标准特性,尤其是在显示列表数据的场景中。`...
"drag-sort-listview"是一个专为Android平台设计的开源库,它允许用户通过拖放操作来排序ListView中的项目。在Android开发中,ListView是展示大量数据的常用组件,但默认情况下,ListView并不支持直接的拖放排序功能...
通过以上步骤,我们就可以在Android应用中实现ListView的第一个Item的默认选中状态,并且在用户交互时动态改变选中状态。记住,优化性能也是关键,合理利用ViewHolder和避免不必要的操作是提升ListView滚动流畅性的...
注意,在上述代码中,`android:state_selected="true"`状态用于定义ListView项被选中时的背景,这表明当ListView的某个item被选中时,会使用tvitembg.xml中定义的选中状态对应的背景图片。 通过上述步骤,即可实现...
《Android专业人士探索ListView深度解析》 在Android应用开发中,ListView是经常被使用的一个组件,它用于展示大量的数据,通常以列表的形式呈现。本资料"android-pro-listview.7z"将带你深入理解ListView的工作...
19Android-16-listviewBaseAdapter.mp4
19Android-15-listviewArray.mp4
•Android---UI篇---Tab Layout(选项卡布局) • •Andorid---UI篇---TableLayout(表格布局) • •Android---UI篇---RelativeLayout(相对布局) • •Android---UI篇---GridView(网格布局) • •Android---UI篇-...
在Android开发中,自定义ListView是一项常见的任务,它允许开发者根据特定需求定制列表项的布局、样式和交互。"android-custom-listview.7z"这个压缩包文件很可能包含了一个示例项目,展示了如何在Android应用中实现...