自定義清單,設定清單背景、清單的列背景、清單的間隔線。
借鑒了一些前輩的代碼。
<a target="_blank" href="http://blog.51cto.com/attachment/201203/142553472.png"></a>
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){
map.put("ItemImage", R.drawable.c);//圖像資源的ID
map.put("ItemTitle", "修改密碼");
}else if(i==2){
map.put("ItemImage", R.drawable.d);//圖像資源的ID
map.put("ItemTitle", "網絡設定");
}else if(i==3){
map.put("ItemTitle", "列印設定");
}else{
map.put("ItemImage", R.drawable.e);//圖像資源的ID
map.put("ItemTitle", "傳回");
}
}
//生成擴充卡的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() {
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
<RelativeLayout
android:id="@+id/RelativeLayout01"
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:id="@+id/ItemImage"
/>
<TextView
android:text="TextView01"
android:layout_marginTop="30px"
android:textSize="20dip"
android:paddingLeft="12dip"
android:textColor="#000000"
android:id="@+id/ItemTitle"
android:layout_toRightOf="@+id/ItemImage"
/>
android:layout_marginTop="20px"
android:layout_alignParentRight="true"
android:id="@+id/last"
</RelativeLayout>
<a target="_blank" href="http://www.devdiv.com/forum.php?mod=viewthread&tid=97913&page=1#pid591587">http://www.devdiv.com/forum.php?mod=viewthread&tid=97913&page=1#pid591587</a>
<a target="_blank" href="http://blog.csdn.net/zeng622peng/archive/2010/08/11/5804965.aspx">http://blog.csdn.net/zeng622peng/archive/2010/08/11/5804965.aspx</a>
<a href="http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=2888">http://www.devdiv.com/home.php?mod=space&uid=14682&do=blog&id=2888</a>
本文轉自xyz_lmn51CTO部落格,原文連結:http://blog.51cto.com/xyzlmn/817375,如需轉載請自行聯系原作者