Package com. gc. tabhost;/*** @ author Android General *** 1. TabHost is a very practical component, and multiple tabs can be conveniently placed in the window, each tab is equivalent to a component * Placement area of the same size as the external container. In this way, you can place more components in a container. * 2. The following components are used in combination with TabHost: * TabWidget: the tab bar. * TabSpec: a Tab page of the Tab. * 3. TabHost is just a simple container. It provides the following two methods to create and add * tabs: * newTabSpec (String tag): Create a tab. * AddTab (TabHost. TabSpec tabSpec): Add a tab. * 4. The general steps for using TabHost are as follows: * (1) define the TabHost component in the interface layout and define the content of the tab for the component * (2) activity should inherit TabActivity * (3) Call the getTabHost () method of TabActivity to obtain the TabHost object * (4) use the TabHost object method to create and add tabs. * 5. Two components must be combined inside the TabHost container: TabWidget and FrameLayout *. The title bar of the TabWidget definition tab: FrameLayout is used to combine multiple options on the "stacked" tab * Page. * 6. Note: * developers may write IDs on their own from time to time. The ids of the TabHost, TabWidget, and FrameLayout components are 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 are referenced by the existing IDs of the Android system. * 7. TabActivity is no longer recommended for the latest Android platform. Instead, it is 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); // obtain 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 the 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 the second tab tabHost. addTab (tab2); TabSpec tab3 = tabHost. newTabSpec ("tab3 "). setIndicator ("Android General 3 "). setContent (R. id. tab03); // Add the third tab tabHost. addTab (tab3 );}}The corresponding xml layout file is:
<FrameLayout android: id = "@ android: id/tabcontent" android: layout_width = "match_parent" android: layout_height = "match_parent">
</FrameLayout>
The program runs as follows:
Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/25346627