
//在layout中,右击新建RelativeLayout排列的xml
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="22dp"
android:src="@drawable/a" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/photo"
android:layout_marginLeft="19dp"
android:layout_toRightOf="@+id/photo"
android:text="name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/name"
android:layout_alignBottom="@+id/name"
android:layout_alignParentRight="true"
android:layout_marginRight="24dp"
android:text="time" />
<TextView
android:id="@+id/neirong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="20dp"
android:text="neirong"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainActivity
package com.example.weibojiemian;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
//MainActivity继承ListActivity
public class MainActivity extends ListActivity {
private List<Map<String, ?>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 获得数据
data = getData();
// 创建SimpleAdapter来装载所需要的数据
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.list_item, new String[] { "name","photo", "time", "content" },
new int[] { R.id.name,R.id.photo, R.id.time, R.id.neirong });
// 将ListView和SimpleAdapter进行绑定
setListAdapter(adapter);
}
private List<Map<String, ?>> getData() {
List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
Map<String, Object> item1 = new HashMap<String, Object>();
item1.put("name", "潇湘夜雨");
item1.put("photo", R.drawable.a);
item1.put("time", "1分钟前");
item1.put("content", "今天天气很好!");
data.add(item1);
Map<String, Object> item2 = new HashMap<String, Object>();
item2.put("name", "小小");
item2.put("photo", R.drawable.b);
item2.put("time", "2分钟前");
item2.put("content", "今天心情很好!");
data.add(item2);
Map<String, Object> item3 = new HashMap<String, Object>();
item3.put("name", "青春无悔");
item3.put("photo", R.drawable.c);
item3.put("time", "3分钟前");
item3.put("content", "今天天气很好!");
data.add(item3);
Map<String, Object> item4 = new HashMap<String, Object>();
item4.put("name", "小丸子");
item4.put("photo", R.drawable.d);
item4.put("time", "4分钟前");
item4.put("content", "今天天气很好!");
data.add(item4);
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}