使用SimpleAdapter创建ListView

来源:互联网 发布:java style 编辑:程序博客网 时间:2024/05/21 17:21

效果图:
这里写图片描述

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"/>

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

0 0
原创粉丝点击