废话不说直接上图
mian.xml 代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!--
属性解释:
android:divider 添加分割线
android:dividerHeight 分割线的高度
android:cacheColorHint
ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉,
原因是,滚动时,列表里面的view重绘时,用的依旧是系统默认的透明色,颜色值为#FF191919,
要改变这种情况,只需要调用listView的setCacheColorHint(0),颜色值设置为0
或者xml文件中listView的属性 Android:cacheColorHint="#00000000"即可,
滚动时,重绘View的时候就不会有背景颜色。
android:listSelector 添加背景选择器
-->
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:dividerHeight="0dip"
android:cacheColorHint="#00000000"
android:id="@+id/itemlist"
android:listSelector="@drawable/list_item_style">
</ListView>
</LinearLayout>
listview.xml 代码如下
<?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="vertical" >
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:background="@drawable/bg"></TextView>
</LinearLayout>
背景选择器 list_item_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true"
android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="@android:color/transparent" />
<item android:state_selected="false"
android:drawable="@android:color/white" />
</selector>
背景选择器《
参考》
java code
public class HandcentActivity extends Activity {
private ListView itemlist;
private List<Map<String, Object>> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemlist = (ListView)findViewById(R.id.itemlist);
refreshListItems();
}
private void refreshListItems() {
list = buildListForSimpleAdapter();
SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.listview,
new String[] { "str" }, new int[] { R.id.TextView01,});
itemlist.setAdapter(notes);
itemlist.setSelection(0);
}
/**
* 模拟适配器加载的数据
* @return
*/
private List<Map<String, Object>> buildListForSimpleAdapter() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(2);
Map<String, Object> map = new HashMap<String, Object>();
map.put("str", "老张,小王没来上班");
list.add(map);
map = new HashMap<String, Object>();
map.put("str", "呵呵");
list.add(map);
map = new HashMap<String, Object>();
map.put("str", "昨天晚上聊天说他很忙,晚上回家有要事要办");
list.add(map);
map = new HashMap<String, Object>();
map.put("str", "你懂的");
list.add(map);
map = new HashMap<String, Object>();
map.put("str", "呵呵呵呵呵呵呵呵呵呵\n呵呵呵呵\n呵呵\n呵呵");
list.add(map);
map = new HashMap<String, Object>();
map.put("str", "呵呵呵呵呵呵呵呵呵呵呵呵呵呵\n呵呵呵呵");
list.add(map);
return list;
}
}
《 代码下载》