<?xml version="1.0" encoding="utf-8"?>
1.布局文件tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android :id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/background">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android :id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
<FrameLayout
android:id="@android :id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</TabHost>
2.用来显示Tab的容器:MainTabActivity.java
2.用来显示Tab
package com.tianlei.test;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
@SuppressWarnings("deprecation")
public class MainTabActivity extends TabActivity {
private Intent testIntent = null;
private Intent waveIntent = null;
private Intent resultIntent = null;
private Intent moreIntent;
TabHost tabHost;
TabSpec spec;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);//这里使用了上面创建的xml文件(Tab页面的布局)
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
initIntent();
addSpec();
tabHost.setCurrentTab(0);
}
/**
* 初始化各个tab标签对应的intent
*/
private void initIntent(){
//初始化intent
testIntent = new Intent(this, ComTestActivity.class);
waveIntent = new Intent(this, MatlabActivity.class);
resultIntent = new Intent(this, ResultActivity.class);
moreIntent = new Intent(this, MoreActivity.class);
}
/**
* 为tabHost添加各个标签项
*/
private void addSpec(){
tabHost.addTab(this.buildTagSpec("test", R.string.test,
R.drawable.test, testIntent));
tabHost.addTab(this.buildTagSpec("wave", R.string.wave,
R.drawable.wave, waveIntent));
tabHost.addTab(this.buildTagSpec("result", R.string.result,
R.drawable.result, resultIntent));
tabHost.addTab(this.buildTagSpec("message",R.string.message,
R.drawable.message,moreIntent));
}
/**
* 自定义创建标签项的
* @param tagName 标签标识
* @param tagLable 标签文字
* @param icon 标签图标
* @param content 标签对应的内容
* @return
*/
private TabHost.TabSpec buildTagSpec(String tagName, int tagLable,
int icon, final Intent content) {
return this.tabHost
.newTabSpec(tagName)
.setIndicator(getResources().getString(tagLable),
getResources().getDrawable(icon))
.setContent(content);
}
}
3.里面有4个Activity,分别对应4个tab。
3.里面 包含四个Activity。分别对应四个tab。