• Android 带图片的列表: 列表布局


    1,编写xml代码

    
    <?xml version="1.0" encoding="utf-8"?>
    <!--
    数据 头部
    -->
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="智慧校园"
            android:textSize="50dp"
            ></TextView>
    
            <ListView
                android:id="@+id/android:list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
    
    
    
    </LinearLayout>
    
    
    <?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/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="26sp"/>
            <TextView
                android:id="@+id/info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="26sp"/>
        </LinearLayout>
    
    </LinearLayout>
    

    2,编写类

    
    package com.example.listviewtest;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity2 extends ListActivity {
        private ListView list;
        private String[] title = {"软件2019级一班","软件2019级二班","软件2019级三班","软件2019级四班","软件2019级五班","软件2019级六班",};
        private String[] info = {"java","html","css","linux"};
        private  int[] imgId ={R.drawable.icon1,R.drawable.icon2,R.drawable.icon3,R.drawable.icon4};
    
        private List<Map<String,Object>> data;
    
    
         private SimpleAdapter adapter;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            list = getListView();
    
            adapter = new SimpleAdapter(this,getData(),R.layout.list_item,new String[]{"img","title","info"},new int[]{R.id.img,R.id.title,R.id.info});
            list.setAdapter(adapter);
    
        }
    
        private  List<Map<String,Object>> getData(){
    //        data 实例化
            data = new ArrayList<Map<String,Object>>();
    
            for (int i = 0;i < imgId.length;i++){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("img",imgId[i]);
                map.put("title",title[i]);
                map.put("info",info[i]);
    
    //            三个数据放入map
                data.add(map);
            }
    
    
            return  data;
    
        }
    }
    

    3,导入图片

    4,项目路径

    5,运行结果截图

  • 相关阅读:
    *.ascx *.asax *.aspx.resx *.asax.resx是什么文件,Global.asax 文件是什么
    SQL Server Profiler的简单使用
    关于web.config配置文件里面的 mode的各种含义,mode="RemoteOnly",mode="On",mode="Windows"
    解决不同js之间冲突windows.onload
    【转】工作需要一个聪明人,工作其实更需要一个踏实的人
    java实现调用c接口
    hdu4043FXTZ II(大数+数学)
    poj1251Jungle Roads
    hdu4034Graph
    poj1236Network of Schools
  • 原文地址:https://www.cnblogs.com/d534/p/14841524.html
Copyright © 2020-2023  润新知