android listView用来显示一个一个的列表
论坛的帖子就可以用它来实现,列表菜单,等等
废话就不多说了,直接上图:
2、布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MyInformation" >
<ListView
android:id="@+id/list_everydayjok"
android:padding="10dp"
android:dividerHeight="10dp"
android:background="#fff"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false">
</ListView>
</LinearLayout>
上面的代码生命了一个listView,间隔10dp
下面是每个listView里面的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_jok" >
<ImageView
android:id="@+id/interes_head01"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/xianjian01" />
<TextView
android:id="@+id/interes_user_name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="@+id/interes_head01"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/interes_head01"
android:gravity="center_vertical"
android:text="@string/uname"
android:textSize="18dp" />
<TextView
android:id="@+id/interes_jok_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/interes_head01"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:text="@string/jokcontent"
android:textSize="18dp" />
<LinearLayout
android:id="@+id/interes_textViewFoot"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/interes_jok_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/interes_textView_ding"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_weight="1"
android:drawableLeft="@drawable/thumb_up"
android:gravity="center_vertical"
android:paddingLeft="30dp"
android:text="@string/thumb_count" />
<TextView
android:id="@+id/interes_textView_comment"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_weight="1"
android:drawableLeft="@drawable/comment"
android:gravity="center_vertical"
android:paddingLeft="30dp"
android:text="@string/thumb_count" />
<TextView
android:id="@+id/interes_textView_share"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_weight="1"
android:drawableLeft="@drawable/share"
android:gravity="center_vertical"
android:paddingLeft="50dp" />
</LinearLayout>
</RelativeLayout>
3、java代码
package com.everydayjok.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.everydayjok.contant.Content;
import com.example.everydayjok.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class EveryDayJok extends Activity {
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.everydayjok);
listview = (ListView)findViewById(R.id.list_everydayjok);
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.list_everydayjok_items,
new String[]{"interes_head01","interes_user_name","interes_jok_content","interes_head01"
,"interes_textView_ding","interes_textView_comment"},
new int[]{R.id.interes_head01,R.id.interes_user_name,R.id.interes_jok_content,
R.id.interes_head01,R.id.interes_textView_ding,R.id.interes_textView_comment});
listview.setAdapter(adapter);
};
public List<Map<String, Object>> getData(){
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String,Object> map = new HashMap<String,Object>();
for(int i=0;i<Content.text.length;i++){
map = new HashMap<String, Object>();
map.put("interes_head01", R.drawable.xianjian02);
map.put("interes_user_name", "XXX");
map.put("interes_jok_content",Content.text[i]);
map.put("interes_head01", R.drawable.xianjian02);
map.put("interes_user_name", "XXX");
map.put("interes_textView_ding",100);
map.put("interes_textView_comment",100);
list.add(map);
}
return list;
}
数组类:
public class Content { public static final String[] text= new String[]{"中新网11月16日电 据美国《侨报》报道,1868年,美国宪法第十四条修正案通过,第一款即规定:“所有在美国出生或在美国归化,并受美国司法管辖的人", "越来越多怀揣“不能让孩子输在起跑线”信念的中国夫妇从中国启程,穿越大洋将新生儿诞生在地球彼端,实现他们的“美国梦”", "电影《中国合伙人》中,邓超扮演的孟晓骏代表上世纪90年代中国大学生对留美的狂热之情。美国于他们,是梦想,是自由、开放的象征;", "数据统计,2007年中国大陆来美生子只有600人,到2010年这一数字增至5000人,而2012年来美生子人数远远超过1万。", "在网络搜索“赴美生子”,有上千条各色月子中心的信息。甚至在淘宝上,“美国月子中心”都有数十条的搜索结果,", "记者联系了一家较为高档的月子中心,它其实不叫月子中心,"}; public static int imageid = R.drawable.xianjian02; }
以上代码可以直接用,经过测试