- TabHost类的简介
TabHost类位于android.widget包下,是选项类的封装,用于创建选项卡窗口。继承自FrameLayout,是帧布局的一种,其中可以包含多个布局,然后根据用户的选择显示不同的界面。继承图如图1
图1
2.TabHost的使用
a.组件:
TabWidget:选项卡按钮,通过多个标签切换不同的内容。
TabSpec:设定选项卡内容的视图
FragmentLayout :代表内容
b.使用:
1)、在布局中定义:在XML中使用TabHost组件,并在其中定义TabWidget和FrameLayout
2)、继承TabActivity(在API 13即Android 3.2前使用)或者FragmentActivity
3)、声明初始化TabHost或通过调用getTabHost方法,获取TabHost对象
4)、通过addTab()方法创建添加选项卡
c.注意:
1)、在XML中使用<TabHost>标签,id需要使用android自带的id:android:id="@android:id/tabhost"
2)、使用<TabWidget>标签时,id需要使用android自带的id:android:id="@android:id/tabhost"
3)、使用<FragmentLayout>标签时,id为:android:id="@android:id/tabcontent"
3.代码
a.XML布局文件
<?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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.basisstudy.view.activity.TabHostActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/linearLayout_one" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <ImageView android:id="@+id/imageView_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" android:layout_gravity="center" android:src="@drawable/one"/> </LinearLayout> <LinearLayout android:id="@+id/linearLayout_two" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <ImageView android:id="@+id/imageView_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" android:layout_gravity="center" android:src="@drawable/two"/> </LinearLayout> <LinearLayout android:id="@+id/linearLayout_three" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <ImageView android:id="@+id/imageView_three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" android:layout_gravity="center" android:src="@drawable/three"/> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>
b.Strings.xml文件:
<resources> <string name="movie">电影</string> <string name="music">音乐</string> <string name="book">书籍</string> </resources>
c.Activity界面代码:
package com.example.administrator.basisstudy.view.activity; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.widget.TabHost; import com.example.administrator.basisstudy.R; /** * @author Administrator */ public class TabHostActivity extends FragmentActivity { private TabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_host); mTabHost = findViewById(android.R.id.tabhost); //初始化TabHost mTabHost.setup(); //添加一个选项 mTabHost.addTab( mTabHost.newTabSpec("音乐") //设置标题 .setIndicator("音乐") //设置内容 .setContent(R.id.linearLayout_one) ); mTabHost.addTab( mTabHost.newTabSpec("电影") //设置标题 .setIndicator("电影") //设置内容 .setContent(R.id.linearLayout_two) ); mTabHost.addTab( mTabHost.newTabSpec("书籍") //设置标题 .setIndicator("书籍") //设置内容 .setContent(R.id.linearLayout_three) ); } }
d.效果图如下: