模拟新浪微博界面

来源:互联网 发布:软件开发职业方向 编辑:程序博客网 时间:2024/05/17 03:24
Android之新浪微博模拟界面
布局文件:
1.在 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:textSize="17sp"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
          />
     <TextView
         android:id="@+id/time"
         android:paddingTop="10dp"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"  
         android:gravity="right"   
/>        
    </LinearLayout>
     <TextView
         android:id="@+id/word"
         android:paddingTop="10dp"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         />
</LinearLayout>
</LinearLayout>
2、MianActivty文件
package cn.wh.listview2;
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.SimpleAdapter;

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[] { "name", "time", "word", "image" }, new int[] {
      R.id.name, R.id.time, R.id.word, R.id.image });
          this.setListAdapter(adapter);
           
}
private void getData() {
        data=new ArrayList<Map<String,?>>();
        Map<String,Object>item1=new HashMap<String, Object>();
        item1.put("name","花溪草");
        item1.put("time","2分钟前");
        item1.put("word","春暖花开,终于可以出去玩啦!!");
        item1.put("image",R.drawable.d);
        data.add(item1);
        Map<String,Object>item2=new HashMap<String, Object>();
        item2.put("name","相约");
        item2.put("time","25分钟前");
        item2.put("word","最近太忙了!");
        item2.put("image",R.drawable.a);
        data.add(item2);
        Map<String,Object>item3=new HashMap<String, Object>();
        item3.put("name","紫玲珑");
        item3.put("time","1小时前");
        item3.put("word","今天好高兴啊!嘻嘻嘻");
        item3.put("image",R.drawable.b);
        data.add(item3);
       
       
}
@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;
}
}
3.如果想自定义标题,在values中建title.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="TitleBackground">
    <item name="android:background">#FF0011</item>
    </style>
   <style name="selfdefine" parent="android:Theme.Light">   //注释:Theme默认为黑色
    <item name="android:windowTitleSize">30dp</item>  
    <item name="android:windowTitleBackgroundStyle">@style/TitleBackground</item>  
</style> 
</resources>
4、然后在AndroidManifest.xml文件中修改


<activity
            android:name="cn.wh.listview2.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>

最终效果:


原创粉丝点击