移动应用开发之高级布局(列表布局)

一、在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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值