<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" >
<!-- 页面中最上面含n个tab的控件 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab0tab0tab0"
android:background="#00aa00"/>
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab1tab1tab1"
android:background="#0000aa"/>
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab2tab2tab2"/>
</FrameLayout>
</LinearLayout>
</TabHost>
package hyz.com.tabwidget;
import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
//TabWidget类似于Android中查看电话簿的界面,通过多个标签切换显示不同的内容
public class TabWidgetTestActivity extends TabActivity implements OnTabChangeListener
{
TabHost th;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//得到对象
th = getTabHost();
// LayoutInflater.from(this).inflate(R.layout.main, th.getTabContentView(), true); 可用此代替setContentView(R.layout.main);
th.addTab(th.newTabSpec("tab0").setIndicator("TAB1",getResources().getDrawable(R.drawable.img1)).setContent(R.id.textview1));
th.addTab(th.newTabSpec("tab1").setIndicator("TAB2",getResources().getDrawable(R.drawable.img2)).setContent(R.id.textview2));
th.addTab(th.newTabSpec("tab2").setIndicator("TAB3",getResources().getDrawable(R.drawable.img3)).setContent(R.id.textview2));
//设置当前显示哪一个标签
th.setCurrentTab(2);
//此会覆盖布局TextView3的设置的背景颜色,其它两个不会
th.setBackgroundColor(Color.argb(150, 150, 300, 20));
//此会覆盖布局TextView3的上面设置中的背景颜色,其它两个不会
th.setBackgroundResource(R.drawable.bg);
//标签切换处理
th.setOnTabChangedListener(this);
}
//tabId为newTabSpec("xxx")里面的字符串值
public void onTabChanged(String tabId)
{
Toast.makeText(this, "哈哈哈"+tabId, Toast.LENGTH_LONG).show();
}
}