1.定义数据源:

private ArrayList<HashMap<String, Object>> sharelist = new ArrayList<HashMap<String, Object>>();

2.初始化数据源:

private void initlist() {
        HashMap<String, Object> list1 = new HashMap<String, Object>();
        list1.put(“icon”, R.drawable.share_via_picture);
        list1.put(“text”, getResources()
                .getString(R.string.share_current_page_via_picture));
        sharelist.add(list1);
        list1 = null;

        HashMap<String, Object> list2 = new HashMap<String, Object>();
        list2.put(“icon”, R.drawable.share_via_pdf);
        list2.put(“text”, getResources()
                .getString(R.string.share_all_pages_via_pdf));
        sharelist.add(list2);
        list2 = null;

        HashMap<String, Object> list3 = new HashMap<String, Object>();
        list3.put(“icon”, R.drawable.share_via_note);
        list3.put(“text”, getResources()
                .getString(R.string.share_current_page_via_note));
        sharelist.add(list3);
        list3 = null;

        HashMap<String, Object> list4 = new HashMap<String, Object>();
        list4.put(“icon”, R.drawable.share_via_note);
        list4.put(“text”, getResources()
                .getString(R.string.share_all_pages_via_note));
        sharelist.add(list4);
        list4 = null;

    }

3.ListView的布局文件:sharepanel.xml

<?xml version=”1.0″ encoding=”utf-8″?>
    <LinearLayout android:id=”@+id/menu_item_container”
        android:layout_width=”match_parent” android:layout_height=”wrap_content”
        android:layout_centerHorizontal=”true” android:orientation=”vertical”
        android:gravity=”center” android:layout_marginTop=”10px”
        android:layout_marginBottom=”2px”>

        <ListView android:id=”@+id/sharelist” android:layout_width=”fill_parent”
            android:cacheColorHint=”#00000000″ android:layout_height=”fill_parent”
            android:divider=”#ffcccccc” android:dividerHeight=”1.0px”>

        </ListView>
    </LinearLayout>

4.ListView的item的布局文件:sharepanel_item.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout android:id=”@+id/linearlayout”
    android:gravity=”center_vertical” android:orientation=”horizontal”
    android:layout_width=”fill_parent” android:layout_height=”wrap_content”
    android:minHeight=”40dip” xmlns:android=”http://schemas.android.com/apk/res/android”
    android:paddingLeft=”30dip” android:paddingRight=”30dip”>

    <ImageView android:id=”@+id/item_icon” android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”></ImageView>

    <TextView android:id=”@+id/item_text” android:layout_width=”wrap_content”
        android:textColor=”@android:color/black” android:textSize=”14dip”
        android:layout_height=”wrap_content”></TextView>
</LinearLayout>

5.开始装配Activity.关键代码

mListView = (ListView) findViewById(R.id.sharelist);

initlist();
SimpleAdapter mSharePanelListAdapter = new SimpleAdapter(
        getContext(), sharelist,// 数据来源
        R.layout.top_menu_share_item,// 每一个user xml 相当ListView的一个组件
        new String[] { “icon”, “text” }, new int[] {
                R.id.item_icon, R.id.item_text }

);
mListView.setAdapter(mSharePanelListAdapter);

6.最终的效果图

listview