實現帶圖示的ListView

never123450發表於2014-06-29

MainAcivity.java

package com.xwy.b;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		ListView listView = (ListView) findViewById(R.id.listView1);
		int[] imageId = new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03,
				R.drawable.img04,R.drawable.img05,R.drawable.img06,R.drawable.img07,
				R.drawable.img08};//定義並初始化儲存圖片id的陣列
		String[] title = new String[]{"保密設定","安全","系統設定","上網","我的文件",
				"GPS導航","我的音樂","E-mail"};//定義並初始化儲存列表項文字的陣列
		List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();//建立一個list集合
		//通過for迴圈將圖片和列表項文字放到Map中,並新增到list集合
		for(int i=0;i<imageId.length;i++){
			Map<String,Object> map = new HashMap<String, Object>();//例項化map物件
			map.put("image", imageId[i]);
			map.put("title", title[i]);
			listItems.add(map);//將map物件新增到list集合中
		}
		SimpleAdapter adapter = new SimpleAdapter(this, listItems,R.layout.items, new String[]{"title", "image"},new int[]{R.id.title,R.id.image} );
		listView.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xwy.b.MainActivity$PlaceholderFragment"
    android:orientation="vertical" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView1" />

</LinearLayout>

items.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" >
    <ImageView 
        android:id="@+id/image"
        android:paddingRight="10px"
        android:paddingBottom="20px"
        android:adjustViewBounds="true"
        android:maxWidth="72px"
        android:maxHeight="72px"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10px"
        android:layout_gravity="center"
        android:id="@+id/title"/>

</LinearLayout>


相關文章