TabHost:
1. Introduction to TabHost
The TabHost component can store multiple tabs on the interface. Many software uses the modified component for design;
1. Common TabHost Components
TabWidget: Click the button in the upper or lower part of the TabHost tab to switch to the tab;
TabSpec: Indicates the tab interface. You can add a TabSpec to TabHost;
--Create Tab: NewTabSpec (String tag) to create a tab;
--Add Tab: AddTab (tabSpec );
2. TabHost usage steps
A.Define Layout: In an XML fileUse the TabHost componentAndDefine a FrameLayout TabContent;
B.Inherit TabActivity: Display the Activity of the tab componentInherit TabActivity;
C.Get component: By callingGetTabHost () methodTo obtain the TabHost object;
D.Create add Tab: Create and add tabs using TabHost;
3. Put the button below
In the layout file, TabWidget represents the tab button and Fragement component represents the content;
Failed to set: If the Fragement component does not set the android: layout_weight attribute, put the TabWidget below, and the button may not be displayed;
Set weight: After the Fragment component weight is set, this tab button is displayed successfully;
Ii. TabHost layout File
1. root tag and id
Set the Android built-in id: XML layout file, which can be used Tag settings. The id must reference android's built-in id: android: id = @ android: id/tabhost;
Prerequisites for getHost () Obtaining: After this id is set, you can use getHost () on the Activity interface to obtain the TabHost view object;
Example:
2. TabWidget Components
Switch tabs: This component is a tab switch button. You can click this component to switch the tab;
Set the android built-in id: Set the id of this component to android's built-in id: android: id = @ android: id/tabs;
TabHost essential components: This component and FrameLayout are two essential components in the TabHost component;
Displayed below the switch button: If you want to put the button below, you can define the component below, but note,FrameLayout: android: layout_widget = 1;
Set the TabWidget size: To set the size of the button component, you can set the weight of the component and FrameLayout component;
Example:
3. FrameLayout component
Component functions: The child component defined in this component is the tab displayed on each page of TabHost. You can define the view displayed on the TabHost tab;
Set the android built-in id: Set the id of this component to android's built-in id: android: id = @ android: id/tabcontent;
Example:
<framelayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent"></framelayout>
Ii. Activity Method
1. Get TabHost
Obtain Method: GetHost ();
Prerequisites: The premise of calling the getHost () method to obtain the TabHost component is to set the android: id = @ android: id/tabhost in the layout file;
2. Create a tab
Create Tab: Call the newTabHost (tag) of the TabHost component, where the tag is a string, that isUnique tab ID;
Settings Tab:
--Set button name: SetIndicator (called Beast );
--Set Tab content: SetContent (). You can set the view component, Activity, or Fragement;
Add Tab: TabHost. add (tag). The input parameters are defined when the tab is created.Unique Identifier;
Code 3
XML layout File:
<framelayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent">
</framelayout>
Main Activity interface code:
Package shuliang. han. tabhost_test; import android. app. tabActivity; import android. OS. bundle; 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. tabhost); TabHost tabHost = getTabHost (); TabSpec page1 = tabHost. newTabSpec (tab1 ). setIndicator (called Beast ). setContent (R. id. isanimal); tabHost. addTab (page1); TabSpec page2 = tabHost. newTabSpec (tab2 ). setIndicator (old wet ). setContent (R. id. alwayswet); tabHost. addTab (page2); TabSpec page3 = tabHost. newTabSpec (tab3 ). setIndicator (which region ). setContent (R. id. nezha); tabHost. addTab (page3 );}}