1.action 必须继承tabActivity,
2.配置xml 文件,TabHost ,TabWidget ,FrameLayout的ID必须分别为@android:id/tabhost,@android:id/tabs,@android:id/tabcontent,同时LinearLayout的android:orientation=”vertical”(LinearLayout默认是横向的)
tab.xml
<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">
<LinearLayout
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"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
package com.maoyang.layout;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class TabWidget extends TabActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
//系统资源文件类
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabSpec spec;
Intent intent; // Reusable Intent for each tab
//第一个TAB
intent = new Intent(this,OneTabActivity.class);//新建一个Intent用作Tab1显示的内容
spec = tabHost.newTabSpec("tab1")//新建一个 Tab
.setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_menu_manage))//设置名称以及图标
.setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx
tabHost.addTab(spec);//添加进tabHost
//第二个TAB
intent = new Intent(this,TwoTabActivity.class);//第二个Intent用作Tab1显示的内容
spec = tabHost.newTabSpec("tab2")//新建一个 Tab
.setIndicator("Tab2", res.getDrawable(android.R.drawable.ic_dialog_map))//设置名称以及图标
.setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx
tabHost.addTab(spec);//添加进tabHost
//tabHost.setBackgroundColor(Color.argb(150,22,70,150));
//设置TabHost的背景图片资源
tabHost.setBackgroundResource(R.drawable.desktop);
tabHost.setOnTabChangedListener(tabChangeListener);
tabHost.setCurrentTab(0);
}
private OnTabChangeListener tabChangeListener=new OnTabChangeListener()
{
public void onTabChanged(String tabId)
{
Toast toast=Toast.makeText(getApplicationContext(), "现在是"+tabId+"标签", Toast.LENGTH_SHORT);
toast.show();
}
};
}