第一步:进行主界面的布局
在layout下新建item.xml,代码如下:
在layout下新建item.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:padding="10dp"
android:layout_width="48dp"
android:layout_height="48dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/publish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:gravity="right" />
</LinearLayout>
<TextView
android:id="@+id/content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
第二步:编写MainActivity并进行相应的事件处理,具体代码如下:
public class MainActivity extends ListActivity {
private List<Map<String,?>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getData();
SimpleAdapter adapter = new SimpleAdapter(this, data,R.layout.item,new String[] { "image","name","publish","content"},
new int[] {R.id.image,R.id.name,R.id.publish,R.id.content});
setListAdapter(adapter);
}
public void getData() {
// TODO Auto-generated method stub
data = new ArrayList<Map<String, ?>>();
Map<String, Object> item = new HashMap<String, Object>();
item.put("image",R.drawable.a );
item.put("name", "︷冰茉冷薇℉");
item.put("publish", "1分钟前");
item.put("content", "拥有宁静的心灵,才能发现意想不到的美。");
data.add(item);
item = new HashMap<String, Object>();
item.put("image",R.drawable.b);
item.put("name", "麽麼茶ō");
item.put("publish", "24分钟前");
item.put("content", "生活坏到一定程度就会好起来,因为它无法更坏。所以我们心中应该总是充满阳光。");
data.add(item);
item = new HashMap<String, Object>();
item.put("image",R.drawable.c );
item.put("name", "﹏藌朵Ω");
item.put("publish", "36分钟前");
item.put("content", "有些人,明明知道爱上会受伤,偏偏要爱;我们一直如此,一边深爱,一边伤害。细细想来,很多的爱,谁不是一边受伤、一边成长。");
data.add(item);
item = new HashMap<String, Object>();
item.put("image",R.drawable.d );
item.put("name", "沫若未央╰╮");
item.put("publish", "47分钟前");
item.put("content", "人生的路,靠的是自己一步步取走,真正能保护你的,是你自己的选择。而真正能伤害你的,也是一样,自己的选择。决定人生的,不是命运,而是你自己的每一次抉择。");
data.add(item);
item = new HashMap<String, Object>();
item.put("image",R.drawable.e );
item.put("name", "懒懒Di喵喵");
item.put("publish", "55分钟前");
item.put("content", "梦想和自由一样,都有代价,但都值得。");
data.add(item);}
@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;
}
}
第三部:自定义标题栏
首先,修改标题栏的高度和背景,在res\values下新建title.xml文件,在其中添加代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TitleBackground">
<item name="android:background">#FF0000</item>
</style>
<style name="selfdefine" parent="android:Theme.Light"> 注:Theme默认为黑色,.Light是白色
<item name="android:windowTitleSize">30dp</item>
<item name="android:windowTitleBackgroundStyle">@style/TitleBackground</item>
</style>
</resources>
然后修改AndroidManifest.xml文件:
<activity
android:name="com.example.weibo.MainActivity"
android:label="@string/app_name"
android:theme="@style/selfdefine"
>"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
效果图: