android:theme="@android:style/Theme.Black.NoTitleBar"
第二步:创建title.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="新浪微博——随便看看"
android:background="#ff6600"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
第三步:建立news.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"
android:layout_marginTop="20dp"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/p1" />
<TextView
android:id="@+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tvName"
android:layout_alignParentRight="true"
android:hint="time"
android:text="" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/imageView1"
android:text="name" />
<TextView
android:id="@+id/tvCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvName"
android:layout_below="@+id/imageView1"
android:text="xinlangwe"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
第四步:在activity_main.xml文件中把刚才创建的title文件包含进来
<include layout="@layout/title"/>
然后在里边添加一个ListView组件,并修改其属性
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
>
</ListView>
package com.example.news;
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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
List<Map<String,?>> data;
ListView listVIew;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=getData();
SimpleAdapter adapter=new SimpleAdapter(this, data,R.layout.news,new String[]{"photo","name","time","content"},new int[]{R.id.imageView1,R.id.tvName,R.id.tvTime,R.id.tvCount});
listVIew=(ListView) this.findViewById(R.id.listView1);
listVIew.setAdapter(adapter);
listVIew.setOnItemClickListener(new as());
}
private class as implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// TODO Auto-generated method stub
Map<String,String> item=(Map<String,String>)data.get(position);
Toast.makeText(MainActivity.this,item.get("name").toString(),Toast.LENGTH_LONG).show();
}
}
private List<Map<String,?>> getData()
{
List<Map<String,?>> data=new ArrayList<Map<String,?>>();
Map<String,Object> item=new HashMap<String,Object>();
item.put("photo",R.drawable.p1);
item.put("name","潇湘夜雨");
item.put("time","1分钟前");
item.put("content","这几天我学会了使用ListView组件。这个组件真的很有用。希望在今后的开发中能大量用到。呵呵学习Android开发。更多的精彩在等待着我们呢……大家加油^_^");
data.add(item);
item=new HashMap<String,Object>();
item.put("photo",R.drawable.p2);
item.put("name","小小");
item.put("time","12分钟前");
item.put("content","哇!这里真好玩。希望在这里结识更多的朋友");
data.add(item);
item=new HashMap<String,Object>();
item.put("photo",R.drawable.p3);
item.put("name","青春无悔");
item.put("time","1小时前");
item.put("content","今天上班累死了,型号下午老板开恩。我们提前下班休息。呵呵^_^");
data.add(item);
item=new HashMap<String,Object>();
item.put("photo",R.drawable.p4);
item.put("name","小丸子");
item.put("time","2小时前");
item.put("content","今天班里大扫除,怎么会有这么多的垃圾,各种无语,看来要好好注意平时的维护了……");
data.add(item);
item=new HashMap<String,Object>();
item.put("photo",R.drawable.p5);
item.put("name","哈哈");
item.put("time","3小时前");
item.put("content","今天心情不好,老是不知道该做些什么");
data.add(item);
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;
}
}