----------------------------------------------XML布局代码---------------------------------
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:adjustViewBounds="true"
android:maxWidth="50dp"
android:maxHeight="50dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textSize="20sp"/>
</LinearLayout>
-----------------------------------------------MainActivity代码------------------------------------
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
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.activity_main);
String[] text=new String[]{"菜单1","菜单2","菜单3","菜单4","菜单5","菜单6","菜单7"};
int[] image=new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03,
R.drawable.img04,R.drawable.img05,R.drawable.img06,R.drawable.img07};
ListView list=(ListView) findViewById(R.id.listView1);
List<Map<String,Object>> items = new ArrayList<Map<String,Object>>(); //创建一个List集合
//通过for循环将text和image数组中的值放到Map中并添加到List集合中
for(int i=0;i<text.length;i++){
Map<String, Object> map=new HashMap<String, Object>();
map.put("image1", image[i]);
map.put("text1", text[i]);
items.add(map); // 将Map得到的对象添加到List集合中
}
SimpleAdapter app=new SimpleAdapter(this, items, R.layout.items,
new String[]{"image1","text1"}, new int[]{R.id.image,R.id.title});
//创建SimpleAdapter适配器---------------R.layout.items指的是layout文件夹下的XML的资源
list.setAdapter(app);
//将适配器与List关联
}
}
-----------运行效果-----------------------