使用SimpleAdapter创建ListView

本文详细介绍了Android应用中UI布局的设计方法,包括主活动布局文件activity_main.xml和聊天列表项布局文件chat_list.xml的具体实现,展示了如何通过LinearLayout进行垂直和水平布局,并通过设置不同属性如padding来调整界面显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:
这里写图片描述

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/TextView1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:textSize="20sp"
        android:text="Hello Guy!"/>

    <ListView 
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#f00"
        android:dividerHeight="2dp">
    </ListView>

</LinearLayout>

chat_list.xml(这里注意几个layout_width和layout_height属性的使用)

<?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="horizontal" >

    <ImageView 
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="vertical">

        <TextView 
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>

        <TextView 
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"/>

    </LinearLayout>
</LinearLayout>

MainActivity.java

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.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    private ListView chatList;
    private int[] icons = new int[]{R.drawable.count,R.drawable.download,R.drawable.me,
            R.drawable.table,R.drawable.write};
    private String[] names = new String[]{"像雾又像风","With You","天天酷跑","跟我一起来","lolCome"};
    private String[] desc = new String[]{"我是一个安静的美男子。","找个也是会员的小姐姐。","没有人能说我不好。",
            "我的天呐,我的钱包居然又掉了!!","欢迎来到,王者荣耀!!"};
    private List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        chatList = (ListView) findViewById(R.id.listview);
        for(int i = 0; i < names.length; i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("icon", icons[i]);
            map.put("name", names[i]);
            map.put("desc", desc[i]);
            lists.add(map);
        }
        SimpleAdapter sa = new SimpleAdapter(this, lists, R.layout.chat_list, 
                new String[]{"icon","name","desc"}, new int[]{R.id.icon,R.id.name,R.id.desc});
        chatList.setAdapter(sa);
    }
}

改变chat_list.xml中ImageView属性:

<ImageView 
    android:id="@+id/icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:paddingLeft="10dp"/>

说明:这里在只写了android:paddingLeft=”10dp”时,会出以下提示

    - Consider adding android:paddingStart="10dp" to better support right-to-left layouts
    - When you define paddingLeft you should probably also define paddingRight for right-to-left 

此时的效果图:
这里写图片描述


为了使界面看上去更和谐,可为右边文字部分加个paddingLeft,此时的效果图:
这里写图片描述


现在paddingLeft和paddingRight双管齐下:

<ImageView 
    android:id="@+id/icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"/>

此时再放效果图:前面的小图标被缩小了
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值