activity_main.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"> <ImageView android:id="@+id/img" android:layout_margin="5dp" android:layout_width="35dp" android:layout_height="35dp"/> <TextView android:id="@+id/tv" android:textSize="20sp" android:layout_marginLeft="5dp" android:layout_marginTop="8dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
list_view.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"> <ListView android:id="@+id/listview" android:divider="@drawable/c" android:dividerHeight="2dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
Mainactivity.java
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; 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 MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_view); //根据id找到ListView ListView listView=findViewById(R.id.listview); //数据 //定义分组 String[] group={"我的好友"}; String[] type=new String[]{"新的朋友","群聊","标签","公众号","傻龙","熊美腻","宝贝婷","黄小田","宝藏晴","珍妮Jan","彤彤"}; //图片路径 int[] imgId = new int[] {R.mipmap.b,R.mipmap.b, R.mipmap.b,R.mipmap.b, R.mipmap.b,R.mipmap.b, R.mipmap.b,R.mipmap.b, R.mipmap.b,R.mipmap.b, R.mipmap.b}; //将数组数据转换到list数据,泛型Map映射 List<Map<String,Object>> list=new ArrayList<Map<String,Object>>(); for (int i=0;i<type.length;i++){ Map<String,Object> map=new HashMap<String,Object>(); map.put("img",imgId[i]); map.put("name",type[i]); list.add(map); } //适配器 SimpleAdapter adapter=new SimpleAdapter(this,list, R.layout.activity_main,new String[]{"img","name"}, new int[]{R.id.img,R.id.tv}); //给列表视图设置适配器 listView.setAdapter(adapter); } }
//自己注意一下图片路径就好了