选项卡组件和ListView组件结合模拟新浪微博
效果图:
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 two three:
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", "小a");
data01.put("address", "夜空中最亮的星");
data01.put("photo",R.drawable.a);
data.add(data01);
data01 = new HashMap<String, Object>();
data01.put("name", "小b");
data01.put("address", "夜空中最亮的星");
data01.put("photo",R.drawable.b);
data.add(data01);
data01 = new HashMap<String, Object>();
data01.put("name", "小c");
data01.put("address", "夜空中最亮的星");
data01.put("photo",R.drawable.c);
data.add(data01);
data01 = new HashMap<String, Object>();
data01.put("name", "小d");
data01.put("address", "夜空中最亮的星");
data01.put("photo",R.drawable.b);
data.add(data01);
return data;
}
}
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;
}
}
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;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_marginTop="50dp"
android:layout_height="fill_parent" />
</RelativeLayout>
</TabHost>
activity_one.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_height="match_parent"
android:layout_width="match_parent">
</ListView>
</RelativeLayout>
activity_two.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Two" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
activity_three.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Three" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
blog.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: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>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">03UI_task1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_one">One</string>
<string name="title_activity_two">Two</string>
<string name="title_activity_three">Three</string>
</resources>