- package com.gc.tabhost;
- /**
- * @author Android将军
- *
- *
- *
- * 1、TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置
- * 多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件
- * 摆放区域。通过这种方式,就可以在一个容器里放置更多组件。
- * 2、与TabHost结合使用的还有如下组件:
- * TabWidget:代表选项卡的标签条。
- * TabSpec:代表选项卡的一个Tab页面。
- * 3、TabHost仅仅是一个简单的容器,它提供了如下两个方法来创建、添加
- * 选项卡:
- * newTabSpec(String tag):创建选项卡。
- * addTab(TabHost.TabSpec tabSpec):添加选项卡。
- * 4、使用TabHost的一般步骤如下:
- * (1)在界面布局中定义TabHost组件,并为该组件定义该选项卡的内容
- * (2)Activity应该继承TabActivity
- * (3)调用TabActivity的getTabHost()方法获取TabHost对象
- * (4)通过TabHost对象的方法来创建、添加选项卡。
- * 5、TabHost容器内部需要组合两个组件:TabWidget和FrameLayout
- * ,其中TabWidget定义选项卡的标题条:FrameLayout则用于“层叠”组合多个选项
- * 页面。
- * 6、注意:
- * 在ID的书写时不时开发者自己书写,TabHost、TabWidget和FrameLayout
- * 这三个组件的ID是有要求的:
- * TabHost的ID应该为@android:id/tabhost
- * TabWidget的ID应该为@android:id/tabs
- * FrameLayout的ID应该为@android:id/tabcontent.
- * 这三个ID不是我们自己定义的,而是引用了Android系统已有的ID。
- * 7、最新版本的Android平台已经不再推荐使用TabActivity,而是推荐使用
- * Fragment来代替TabActivity。
- */
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.TabActivity;
- import android.view.Menu;
- import android.widget.TabHost;
- import android.widget.TabHost.TabSpec;
- public class MainActivity extends TabActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //获取该Activity里面的TabHost组件
- TabHost tabHost=getTabHost();
- //创建第一个Tab页
- TabSpec tab1=tabHost.newTabSpec("tab1")
- .setIndicator("Android将军1")
- .setContent(R.id.tab01);
- //添加第一个标签页
- tabHost.addTab(tab1);
- TabSpec tab2=tabHost.newTabSpec("tab2")
- .setIndicator("Android将军2",getResources().getDrawable(R.drawable.ic_launcher))
- .setContent(R.id.tab02);
- //添加第二个标签页
- tabHost.addTab(tab2);
- TabSpec tab3=tabHost.newTabSpec("tab3").setIndicator("Android将军3")
- .setContent(R.id.tab03);
- //添加第三个标签页
- tabHost.addTab(tab3);
- }
- }
package com.gc.tabhost;
/**
* @author Android将军
*
*
*
* 1、TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置
* 多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件
* 摆放区域。通过这种方式,就可以在一个容器里放置更多组件。
* 2、与TabHost结合使用的还有如下组件:
* TabWidget:代表选项卡的标签条。
* TabSpec:代表选项卡的一个Tab页面。
* 3、TabHost仅仅是一个简单的容器,它提供了如下两个方法来创建、添加
* 选项卡:
* newTabSpec(String tag):创建选项卡。
* addTab(TabHost.TabSpec tabSpec):添加选项卡。
* 4、使用TabHost的一般步骤如下:
* (1)在界面布局中定义TabHost组件,并为该组件定义该选项卡的内容
* (2)Activity应该继承TabActivity
* (3)调用TabActivity的getTabHost()方法获取TabHost对象
* (4)通过TabHost对象的方法来创建、添加选项卡。
* 5、TabHost容器内部需要组合两个组件:TabWidget和FrameLayout
* ,其中TabWidget定义选项卡的标题条:FrameLayout则用于“层叠”组合多个选项
* 页面。
* 6、注意:
* 在ID的书写时不时开发者自己书写,TabHost、TabWidget和FrameLayout
* 这三个组件的ID是有要求的:
* TabHost的ID应该为@android:id/tabhost
* TabWidget的ID应该为@android:id/tabs
* FrameLayout的ID应该为@android:id/tabcontent.
* 这三个ID不是我们自己定义的,而是引用了Android系统已有的ID。
* 7、最新版本的Android平台已经不再推荐使用TabActivity,而是推荐使用
* Fragment来代替TabActivity。
*/
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取该Activity里面的TabHost组件
TabHost tabHost=getTabHost();
//创建第一个Tab页
TabSpec tab1=tabHost.newTabSpec("tab1")
.setIndicator("Android将军1")
.setContent(R.id.tab01);
//添加第一个标签页
tabHost.addTab(tab1);
TabSpec tab2=tabHost.newTabSpec("tab2")
.setIndicator("Android将军2",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.tab02);
//添加第二个标签页
tabHost.addTab(tab2);
TabSpec tab3=tabHost.newTabSpec("tab3").setIndicator("Android将军3")
.setContent(R.id.tab03);
//添加第三个标签页
tabHost.addTab(tab3);
}
}
相应的xml布局文件为:
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- </RelativeLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <!-- 定义第一个标签页的内容 -->
- <LinearLayout
- android:id="@+id/tab01"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Android将军"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="赳赳老秦,共赴国难,秦朝将军"
- />
- </LinearLayout>
- <!-- 定义第二个标签页的内容 -->
- <LinearLayout
- android:id="@+id/tab02"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Android将军2"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="赳赳老秦,共赴国难,秦朝将军2"
- />
- </LinearLayout>
- <!-- 定义第三个标签页的内容 -->
- <LinearLayout
- android:id="@+id/tab03"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Android将军3"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="赳赳老秦,共赴国难,秦朝将军3"
- />
- </LinearLayout>
- </FrameLayout>
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- />
- </RelativeLayout>
- </LinearLayout>
- </TabHost>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- 定义第一个标签页的内容 -->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android将军"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="赳赳老秦,共赴国难,秦朝将军"
/>
</LinearLayout>
<!-- 定义第二个标签页的内容 -->
<LinearLayout
android:id="@+id/tab02"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android将军2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="赳赳老秦,共赴国难,秦朝将军2"
/>
</LinearLayout>
<!-- 定义第三个标签页的内容 -->
<LinearLayout
android:id="@+id/tab03"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android将军3"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="赳赳老秦,共赴国难,秦朝将军3"
/>
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</LinearLayout>
</TabHost>
程序运行效果图为: