利用ListView,按下图方式显示人物信息,包括图片、姓名、别名等。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.hades.ListViews.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</android.support.constraint.ConstraintLayout>
listview.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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="76dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="65dp"
android:layout_height="61dp"
android:maxWidth="50dp"
android:maxHeight="50dp"
android:layout_gravity="center"
app:srcCompat="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:text="TextView"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:text="TextView"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.hades.ListViews;
import android.support.v7.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 {
private ListView listView;
//private List<String> data = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.list);
//listView = (ListView)findViewById(R.id.)
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.listview,
new String[]{"title","info","img"},
new int[]{R.id.title,R.id.info,R.id.img});
listView.setAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "阿鲁1");
map.put("info", "这是第一个阿鲁表情");
map.put("img", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "阿鲁2");
map.put("info", "这是第二个阿鲁表情");
map.put("img", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "阿鲁3");
map.put("info", "这是第三个阿鲁表情");
map.put("img", R.drawable.i3);
list.add(map);
return list;
}
}