结果图如下:
MainActivity.java
package com.example.task2.activity; import android.os.Bundle; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.view.Menu; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity { TabHost tabHost; TabSpec tabSpec01,tabSpec02,tabSpec03; Intent intent01,intent02,intent03; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab); tabHost=getTabHost(); intent01 = new Intent(this, One.class); intent02 = new Intent(this, Two.class); intent03= new Intent(this, Three.class); tabSpec01 = tabHost.newTabSpec("system").setIndicator("Blog",null). setContent(intent01); tabSpec02 = tabHost.newTabSpec("hardware").setIndicator("Test",null). setContent(intent02); tabSpec03 =tabHost.newTabSpec("apple").setIndicator("bag",null). setContent(intent03); tabHost.addTab(tabSpec01); tabHost.addTab(tabSpec02); tabHost.addTab(tabSpec03); tabHost.setCurrentTab(0); } }one.java
package com.example.task2.activity; 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.widget.ListView; import android.widget.SimpleAdapter; public class One extends Activity { private ListView listView; List<Map<String, ?>> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_one); listView = (ListView)findViewById(R.id.lv); SimpleAdapter simpleAdapter = new SimpleAdapter(One.this, getData(), R.layout.blog, new String[]{ "name","address","photo"}, new int[]{R.id.name,R.id.wenzi,R.id.photo}); listView.setAdapter(simpleAdapter); } public List<Map<String, ?>> getData() { data = new ArrayList<Map<String, ?>>(); Map<String, Object> data01 = new HashMap<String, Object>(); data01.put("name", "云影"); data01.put("address", "今天心情不错,天气也挺好"); data01.put("photo",R.drawable.aabb); data.add(data01); data01 = new HashMap<String, Object>(); data01.put("name", "星星"); data01.put("address", "好想去马尔代夫旅游啊"); data01.put("photo",R.drawable.aabb); data.add(data01); data01 = new HashMap<String, Object>(); data01.put("name", "月亮"); data01.put("address", "超级喜欢金秀贤"); data01.put("photo",R.drawable.aabb); data.add(data01); data01 = new HashMap<String, Object>(); data01.put("name", "宁远"); data01.put("address", "今天想吃肉"); data01.put("photo",R.drawable.aabb); data.add(data01); return data; } }
package com.example.task2.activity; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class Two extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.two, menu); return true; } }
package com.example.task2.activity; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class Three extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.three, menu); return true; } }布局文件
<?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:orientation="vertical" > <ImageButton android:id="@+id/photo" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginTop="20dp" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/photo" android:text="" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/name" android:layout_marginLeft="130dp" android:layout_marginTop="20dp" android:text="1分钟前" /> <TextView android:id="@+id/wenzi" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_below="@id/name" android:layout_toRightOf="@id/photo" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:text=""/> </RelativeLayout>