<TabHostxmlns: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"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<TextView
android:id="@+id/textview01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is one tab!"
/>
<TextView
android:id="@+id/textview02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is tow tab!"
/>
<TextView
android:id="@+id/textview03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is third tab!"
/>
FrameLayout>
LinearLayout>
TabHost>
2.TestTabWidgetActivity:
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class TestTabWidgetActivity extends TabActivity
{
private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得TabHost对象
mTabHost = getTabHost();
// 为TabHost添加标签
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1",
getResources().getDrawable(R.drawable.flag)).setContent(R.id.textview01));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2",
getResources().getDrawable(R.drawable.flag2)).setContent(R.id.textview02));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3",
getResources().getDrawable(R.drawable.flag3)).setContent(R.id.textview03));
// 设置TabHost背景颜色
// mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
// 设置TabHost的背景图片资源
// mTabHost.setBackgroundResource(R.drawable.icon);
// 设置当前显示哪一个标签setOnTabChangedListener
mTabHost.setCurrentTab(0);
// 标签处理事件
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{
Dialog dialog = newAlertDialog.Builder(TestTabWidgetActivity.this).setTitle("提示").setMessage(
"当前选择的是" + tabId + "号标签").setPositiveButton("确定", newOnClickListener()
{
@Override
public voidonClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
}).create();// 创建按钮
dialog.show();
}
});
3.效果图:
软件运行: