Package com.gc.tabhost;/** * @author Android General * * * * 1, Tabhost is a very useful component, tabhost can easily place on the window * Multiple tabs, each tab is equivalent to the acquisition of an external The container is the same size of the component * placement area. In this way, more components can be placed in a container. * 2, in combination with tabhost, there are the following components: * Tabwidget: The tab bar representing the tabs. * Tabspec: Represents a tab page for tabs. * 3, Tabhost is only a simple container, it provides the following two ways to create, add the * tab: * NEWTABSPEC (String tag): Create tab. * ADDTAB (Tabhost.tabspec tabspec): Add tab. * 4, the general steps to use Tabhost are as follows: * (1) Define the Tabhost component in the interface layout and define the contents of the tab for the component * (2) activity should inherit tabactivity * (3) Call Tabactivity's Gettabhost () method to get the Tabhost object * (4) To create and Add tabs by means of Tabhost objects. * 5, Tabhost container internal need to combine two components: Tabwidget and Framelayout *, where Tabwidget defines the title bar of the tab: Framelayout is used to "cascade" the combination of multiple options * pages. * 6, Note: * In the writing of the ID from time to time developers write their own, tabhost, Tabwidget and Framelayout * These three components of the ID is required: * tabhost ID should be @android:id/tabhost * Tabwidget ID should be @android:id/tabs * framelayout ID should be @android:id/tabcontent. * These three IDs are not defined by ourselves, but instead refer to the existing IDs of the Android system. * 7, the latest version of the Android platform is no longer recommended to use tabactivity, but recommended to use * fragment instead of 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 {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//Get the Tabhost component Tabhost Tabhost=gettabhost in the activity ( );//Create the first tab page Tabspec tab1=tabhost.newtabspec ("Tab1"). Setindicator ("Android General 1"). SetContent (R.ID.TAB01);// Add first tab Tabhost.addtab (TAB1); Tabspec Tab2=tabhost.newtabspec ("TaB2"). Setindicator ("Android General 2", Getresources (). Getdrawable (R.drawable.ic_ Launcher). SetContent (R.ID.TAB02);//Add a second tab tabhost.addtab (TAB2); Tabspec Tab3=tabhost.newtabspec ("Tab3"). Setindicator ("Android General 3"). SetContent (R.ID.TAB03);// Add a third tab tabhost.addtab (TAB3);}}
The corresponding XML layout file is:
<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_wid Th= "Fill_parent" android:layout_height= "fill_parent" > </RelativeLayout> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" &G T <relativelayout android:layout_width= "fill_parent" android:layout_height= "Fill_parent" &G T <framelayout android:id= "@android: Id/tabcontent" android:layout_width= "Match_parent" Androi d:layout_height= "Match_parent" > <!--define the contents of the first tab--<linearlayout a Ndroid:id= "@+id/tab01" android:orientation= "vertical" android:layout_width= "fill_parent" Android:layout_height= "Fill_parent" > <textview android:layout_width= "Fill_pa Rent "android:layout_height=" wrap_content "android:text=" Android General "/&G T <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "Jiujiu Lao Qin, a total of national calamity, Qin Dynasty general"/> </LinearLayout> <!--definition Second tab CONTENTS--<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_cont Ent "android:text=" Android General 2 "/> <textview Android : Layout_Width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Jiujiu Lao Qin, altogether to national calamity, Qin Dynasty general 2" /> </LinearLayout> <!--define the contents of the Third tab--<linearlayout Android:id= "@+id/tab03" android:orientation= "vertical" android:layout_width= "fill_pare NT "android:layout_height=" Fill_parent "> <textview Android : layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "Android General 3 "/> <textview android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:text= "Jiujiu Lao Qin, altogether to national calamity, Qin Dynasty general 3"/> </ linearlayout> </FrameLayout> <tabwidget android:id= "@android: Id/tabs" and Roid:layout_width= "Match_parent "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true "/> </RelativeLayout> </LinearLayout></TabHost>
The program runs as follows:
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/25346627