Android笔记—Listview控件的自定义使用

首先我们要创建一个listitems.xml布局文件,该布局文件是用来显示每行的显示效果,该文件中包含两个控件,一个imageview控件和一个textview控件。具体代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <ImageView
            android:id="@+id/img"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="3dp"
            android:src="@drawable/button_above_more" />


        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="24sp" />


    </LinearLayout>


</LinearLayout>




接下来就可以在Activity中进行下面步骤了。


1、首先获取标识符,定义

ListView menulist=(ListView)findViewById(R.id.listView_Menu);




2、定义Listview控件菜单项
String[] text={getResources().getString(R.string.menu_item_play),
		getResources().getString(R.string.menu_item_scores),
		getResources().getString(R.string.menu_item_settings),
		getResources().getString(R.string.menu_item_help)
		};
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String,Object>>();




3、设置每个选项行前面的图片
for (int i = 0; i < 4; i++){
			HashMap<String, Object> item = new HashMap<String, Object>();
			switch (i) {
			case 0:
				item.put("img", R.drawable.menu_item_play);
				item.put("text", text[i]);
				items.add(item);
				break;
			case 1:
				item.put("img", R.drawable.menu_item_scores);
				item.put("text", text[i]);
				items.add(item);
				break;
			case 2:
				item.put("img", R.drawable.menu_item_settings);
				item.put("text", text[i]);
				items.add(item);
				break;
			case 3:
				item.put("img", R.drawable.menu_item_help);
				item.put("text", text[i]);
				items.add(item);
				break;
			default:
				break;
			}
		}




4、配置适配器
SimpleAdapter si = new SimpleAdapter(this, items,
				R.layout.listitems, new String[] { "img", "text" },
				new int[] { R.id.img, R.id.text });
		ListView l = (ListView) findViewById(R.id.listView1);
		l.setAdapter(si);


5、添加每个选项的点击事件
l.setOnItemClickListener(new OnItemClickListener() {


			@Override
			public void onItemClick(AdapterView<?> parent, View itemClicked,
					int position, long id) {
				switch (position) {
				case 0:


					break;
				case 1:


					break;
				case 2:


					break;
				case 3:


					break;


				default:
					break;
				}
			}


		});


完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值