一、在activity_main.xml文件中定义
<ListView
android:layout_width="match_parent"
android:id="@+id/listView"
android:layout_height="match_parent" />
二、在layout下再创建一个布局文件(直接右键添加layout——resource文件) layout_width="0dp"保证文字多少不影响宽度,三块均分则是由layout_weight保证
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/text01"
android:textSize="20dp"
android:background="#ffff7d3a"
android:layout_weight="1" />
<TextView
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/text02"
android:textSize="20dp"
android:layout_width="0dp"
android:background="#ffffbd28"
android:layout_weight="1" />
<TextView
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/text03"
android:textSize="20dp"
android:layout_width="0dp"
android:background="#ffff1e2a"
android:layout_weight="1" />
</LinearLayout>
三、mainactivity.java文件中
使用hashmap来接收数据,并且定义一个适配器,对应着text把数据填入
list = (ListView) findViewById(R.id.listView);
List<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
for (int i=0; i<5; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("itemId", String.valueOf(IDS[i]));
map.put("itemName", NAMES[i]);
map.put("itemMajor", MAJORS[i]);
myList.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, myList, R.layout.item_list, new String[] {"itemId", "itemName", "itemMajor"}, new int[] {R.id.text01, R.id.text02, R.id.text03});
list.setAdapter(adapter);