您的位置:首页 > 产品设计 > UI/UE

Android UI开发第五篇——自定义列表

2011-10-26 11:04 393 查看
自定义列表,设置列表背景、列表的列背景、列表的间隔线。

借鉴了一些前辈的代码。





MainActivity.class

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定Layout里面的ListView
ListView list = (ListView) findViewById(R.id.ListView01);

//生成动态数组,加入数据
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
for(int i=0;i<5;i++)
{
if(i==0){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.checked);//图像资源的ID
map.put("ItemTitle", "个人信息");
map.put("LastImage", R.drawable.lastimage);
listItem.add(map);
}else if(i==1){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.c);//图像资源的ID
map.put("ItemTitle", "修改密码");
map.put("LastImage", R.drawable.lastimage);
listItem.add(map);
}else if(i==2){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.d);//图像资源的ID
map.put("ItemTitle", "网络设置");
map.put("LastImage", R.drawable.lastimage);
listItem.add(map);
}else if(i==3){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.d);//图像资源的ID
map.put("ItemTitle", "打印设置");
map.put("LastImage", R.drawable.lastimage);
listItem.add(map);
}else{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.e);//图像资源的ID
map.put("ItemTitle", "返回");
map.put("LastImage", R.drawable.lastimage);
listItem.add(map);
}

}
//生成适配器的Item和动态数组对应的元素
SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,// 数据源
R.layout.list_items,//ListItem的XML实现
//动态数组与ImageItem对应的子项
new String[] {"ItemImage","ItemTitle", "LastImage"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.last}
);

//添加并且显示
list.setAdapter(listItemAdapter);

//添加点击
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第"+arg2+"个项目");
if(arg2 == 4){
MainActivity.this.finish();
}
}
});

//添加长按点击
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
menu.setHeaderTitle("长按菜单-ContextMenu");
menu.add(0, 0, 0, "弹出长按菜单0");
menu.add(0, 1, 0, "弹出长按菜单1");
}
});
}

//长按菜单响应函数
@Override
public boolean onContextItemSelected(MenuItem item) {
setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
return super.onContextItemSelected(item);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ListView01"
android:divider="@drawable/divider_color"
android:dividerHeight="3dip"
android:cacheColorHint="#00000000"
/>
</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip"
android:background="@drawable/list_bg">
<ImageView
android:paddingTop="12dip"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ItemImage"
/>
<TextView
android:text="TextView01"
android:layout_height="wrap_content"
android:layout_marginTop="30px"
android:textSize="20dip"
android:paddingLeft="12dip"
android:textColor="#000000"
android:layout_width="fill_parent"
android:id="@+id/ItemTitle"
android:layout_toRightOf="@+id/ItemImage"
/>
<ImageView
android:paddingTop="12dip"
android:layout_marginTop="20px"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/last"
/>
</RelativeLayout>

http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=2888http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=2888http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=2888
http://www.devdiv.com/forum.php?mod=viewthread&tid=97913&page=1#pid591587
http://blog.csdn.net/zeng622peng/archive/2010/08/11/5804965.aspx

http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=2888

/**

* @author 张兴业

* 邮箱:xy-zhang@163.com

* qq:363302850

*

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息