Tabhost控件又称分页控件,在很多的开发语言中都存在。它可以拥有多个标签页,每个标签页可以拥有不同的内容。android中,一个标签页可以放 一个view或者一个activity。TabHost是标签控件类的核心类,也是标签的集合。
1.tabhost定义
android控件中有封装好的tab控件,直接拖一个到xml文件中。下面的代码都是tab控件自己生成的。
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TabHost 12 android:id="@android:id/tabhost" 13 android:layout_width="fill_parent" 14 android:layout_height="fill_parent" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentTop="true" > 17 18 <LinearLayout 19 android:layout_width="match_parent" 20 android:layout_height="match_parent" 21 android:orientation="vertical" > 22 23 <TabWidget 24 android:id="@android:id/tabs" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" > 27 </TabWidget> 28 29 <FrameLayout 30 android:id="@android:id/tabcontent" 31 android:layout_width="match_parent" 32 android:layout_height="match_parent" > 33 34 <LinearLayout 35 android:id="@+id/tab1" 36 android:layout_width="match_parent" 37 android:layout_height="match_parent" 38 android:orientation="vertical" > 39 40 </LinearLayout> 41 42 <LinearLayout 43 android:id="@+id/tab2" 44 android:layout_width="match_parent" 45 android:layout_height="match_parent" 46 android:orientation="vertical" > 47 48 </LinearLayout> 49 50 <LinearLayout 51 android:id="@+id/tab3" 52 android:layout_width="match_parent" 53 android:layout_height="match_parent" 54 android:orientation="vertical" > 55 56 </LinearLayout> 57 </FrameLayout> 58 </LinearLayout> 59 </TabHost> 60 61 </RelativeLayout>