先来看一下运行图:
接下来,让我们看看具体的代码:
package com.example.tab;
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.TabHost;
//继承TabActivity
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// 获取选项卡组
TabHost tabHost = getTabHost();
// 引用布局文件
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.activity_main, tabHost.getTabContentView());
Resources resources = getResources();
Intent intent=new Intent();
intent.setClass(this,Tab01Activity.class);
// 创建一个tab对象
//TabHost.TabSpec tab01 = tabHost.newTabSpec("tab01").setIndicator("选项一").setContent(intent);
TabHost.TabSpec tab01 = tabHost.newTabSpec("tab01").setIndicator("",
resources.getDrawable(R.drawable.a)).setContent(intent);
Intent intent02=new Intent();
intent02.setClass(this,Tab02Activity.class);
TabHost.TabSpec tab02 = tabHost.newTabSpec("tab02").setIndicator("",
resources.getDrawable(R.drawable.b)).setContent(intent02);
TabHost.TabSpec tab03 = tabHost.newTabSpec("tab03").setIndicator("",
resources.getDrawable(R.drawable.c)).setContent(R.id.textView3);
// 将创建好的tab放入tabHost中
tabHost.addTab(tab01);
tabHost.addTab(tab02);
tabHost.addTab(tab03);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Tab01Activity.java
package com.example.tab;
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.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Tab01Activity extends Activity {
private ListView listView;
private List<Map<String, ?>> data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab01);
// 查找组件
listView = (ListView) this.findViewById(R.id.listView);
data = getData();
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.tab01_item, new String[] { "photo", "name", "publish",
"content" }, new int[] { R.id.photo, R.id.name,
R.id.publish, R.id.content });
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ListViewHandler());
}
private class ListViewHandler implements OnItemClickListener {
public void onItemClick(AdapterView<?> listView, View view, int position,
long id) {
Map<String,?> item=data.get(position);
Toast.makeText(Tab01Activity.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.a);
item.put("name", "45度的心情");
item.put("publish", "1分钟前");
item.put("content", " 等待并不可怕,可怕的是不知道何时是个尽头");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.b);
item.put("name", "myMint");
item.put("publish", "2分钟前");
item.put("content", "如果世界上曾经有那个人出现过,其他人都会变成将就");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.c);
item.put("name", "哈哈哈");
item.put("publish", "3分钟前");
item.put("content", "平静是因为已经有所决定");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.d);
item.put("name", "嘿嘿嘿");
item.put("publish", "4分钟前");
item.put("content", "悄悄,是离别的笙箫,沉默,是今晚的康桥.");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.e);
item.put("name", "呵呵呵");
item.put("publish", "10分钟前");
item.put("content", "有些人的伤口是在时间中慢慢痊愈,如我。 有些人的伤口是在时间中慢慢溃烂,如他。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.f);
item.put("name", "嘻嘻嘻");
item.put("publish", "20分钟前");
item.put("content", "等待并不可怕,可怕的是不知道何时是个尽头");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.h);
item.put("name", "呦呦呦");
item.put("publish", "30分钟前");
item.put("content", "其实等待与时间无关,它是一种习惯,它自由生长,而他无法抑制");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.i);
item.put("name", "想想");
item.put("publish", "40分钟前");
item.put("content", "结局已经如此,原因已经不再重要了");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.a);
item.put("name", "二zhi");
item.put("publish", "50分钟前");
item.put("content", "没有缓冲,跳掉了所有的过程,却跳不掉分离多年造成的生疏和难解的心结");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.b);
item.put("name", "丸子");
item.put("publish", "1个小时前");
item.put("content", "一人花开,一人花落,这些年从头到尾,无人问询");
data.add(item);
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Tab02Activity.java
package com.example.tab;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class Tab02Activity extends Activity {
// 设置当前图片的索引
private int index;
// 定义图片数组
private int[] images = new int[] { R.drawable.b1, R.drawable.b2,
R.drawable.b3, R.drawable.b4, R.drawable.b5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏设置
requestWindowFeature(Window.FEATURE_NO_TITLE);// 无标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.tab02);
// 得到ImageSwitcher对象
final ImageSwitcher switcher = (ImageSwitcher) this
.findViewById(R.id.imageSwitcher1);
// 实现并设置工厂内部接口的makeView方法,用来显示视图
switcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(Tab02Activity.this);
}
});
// 设置图片来源
switcher.setImageResource(images[index]);
// 设置点击事件
switcher.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
index++;
if (index >= images.length) {
index = 0;
}
switcher.setImageResource(images[index]);
}
});
// 设置切入动画
switcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
// 设置切出动画
switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
}
}
activity_main.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" >
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello!Android" />
</RelativeLayout>
tab01_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#55000000" >
<ImageView
android:id="@+id/photo"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="10dp" />
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp" />
<TextView
android:id="@+id/publish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="11dp"/>
</LinearLayout>
<TextView
android:id="@+id/empty"
android:layout_width="10dp"
android:layout_height="10dp"/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
tab01.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" >
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
tab02.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#55000000">
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageSwitcher>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">Tab</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">Tab组件与ListView组件的应用</string>
</resources>
有一点很重要,要在AndroidManifest.xml中设置一下:
<activity android:name=".Tab01Activity"></activity>
<activity android:name=".Tab02Activity"></activity>
这点很重要!!