在布局文件中,首先添加一个TabHost组件,然后在该组件中添加线性布局管理器,并且在该布局中添加一个作为标签组的TabWidget和一个作为标签内容的FrameLayout组件。
res/layout/main.xml:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
编写各标签页中要显示内容对应的XML布局文件:
res/layout/tab1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="界面1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎来到界面1"/>
</LinearLayout>
res/layout/tab2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="界面2"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎来到界面2"/>
</LinearLayout>
Activity代码:
protected void onCreate(){
tabHost.setup();
LayoutInflater inflater = LayoutInflater.from(mContext);
inflater.inflate(R.layout.tab1_fxjk,tabHost.getTabContentView());
inflater.inflate(R.layout.tab2_fxjk,tabHost.getTabContentView());
tabHost.addTab(tabHost.newTabSpec("tab01")
.setIndicator("航班列表")
.setContent(R.id.linearLayout1));//添加第一个标签页
tabHost.addTab(tabHost.newTabSpec("tab02")
.setIndicator("航班信息")
.setContent(R.id.linearLayout2));//添加第二个标签页
tabHost.setCurrentTabByTag("航班列表"); // 设置第一次打开时默认显示的标签,
updateTab(tabHost);//初始化Tab的颜色,和字体的颜色
tabHost.setOnTabChangedListener(new OnTabChangedListener()); // 选择监听器
}
class OnTabChangedListener implements TabHost.OnTabChangeListener {
@Override
public void onTabChanged(String tabId) {
tabHost.setCurrentTabByTag(tabId);
System.out.println("tabid " + tabId);
System.out.println("curreny after: " + tabHost.getCurrentTabTag());
updateTab(tabHost);
}
}
/**
* 更新Tab标签的颜色,和字体的颜色
* @param tabHost
*/
private void updateTab(final TabHost tabHost) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
View view = tabHost.getTabWidget().getChildAt(i);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextSize(9);
tv.setTypeface(Typeface.SERIF, 2); // 设置字体和风格
if (tabHost.getCurrentTab() == i) {//选中
view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_y));//选中后的背景
tv.setTextColor(mContext.getResources().getColorStateList(
android.R.color.black));
} else {//不选中
view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_b));//非选择的背景
tv.setTextColor(mContext.getResources().getColorStateList(
android.R.color.white));
}
}
}
效果如下: