注意该类继承的是 TabActivity
package com.crazyit.ui.tabhostdemo;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
/**
* TabHost的 使用
*/
public class TabHostActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host);
TabHost tabHost = getTabHost();
TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1").
setIndicator("已接电话")
.setContent(R.id.tab01);
tabHost.addTab(tab1);
TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2")
.setIndicator("呼出电话",getResources().getDrawable(R.mipmap.ic_launcher))
.setContent(R.id.tab02);
tabHost.addTab(tab2);
TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3")
.setIndicator("已接电话")
.setContent(R.id.tab03);
tabHost.addTab(tab3);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost" //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:context="com.crazyit.ui.tabhostdemo.TabHostActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs" //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent" //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab01" android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="八部天龙"
android:textSize="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="东海三太子"
android:textSize="22dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="斗战圣佛"
android:textSize="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="花果山水帘洞"
android:textSize="22dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="净坛使者"
android:textSize="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天蓬元帅"
android:textSize="22dp" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
- TabHost容器 需要两个组件 TabWidget和 Framelayout
- FrameLayout 用于 层叠组合多个选项卡
- 布局文件的 id也是有要求的
- TabHost 的 ID 为@android:id/tabhost
- TabWidget的 ID为 @android:id/tabs
- FrameLayout的 Id @android:id/tabcontent