In mobile phone screens, tabs are also commonly used. They are usually combined with lists, for example, the mailing records of our mobile phones. The structure of the Tag is shown below.
TabHost is the container of the entire Tab, which consists of TabWidget and FrameLayout. TabWidget is the label of each tab, and FrameLayout is the tab content.
- If we use extends TabAcitivty, like ListActivity, TabHost must be set to @ android: id/tabhost
- TabWidget must be set to android: id @ android: id/tabs
- For FrameLayout, you need to set android: id to @ android: id/tabcontent.
Example 1: Basic tag example
1) Android XML file
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabhostXmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/c92_tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Tabwidget Android: Id = "@ Android: ID/tabs"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<Framelayout Android: Id = "@ Android: ID/tabcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<AnalogClock android: id = "@ + id/c92_tab1"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_centerHorizontal = "true"/>
<Button android: id = "@ + id/c92_tab2"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "A semi-random Button"/>
</FrameLayout>
</LinearLayout>
</TabHost>
2) source code
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. chapter_9_test2 );
// Step 1: Obtain the TabHost object and initialize setup ()
TabhostTabs = (TabHost) findViewById (R. id. c92_tabhost );
Tabs. Setup ();
// Step 2: Add a tab page through TabHost. TabSpec, add content through setContent (), and add a tab through setIndicator
/* (1) add page 1st */
Tabhost. tabspecSpec = tabs. newTabSpec ("Tag1 ");
Spec.Setcontent(R. id. c92_tab1 );
Spec.Setindicator("Clock ");
Tabs.Addtab(Spec );
/* (2) add page 2nd */
Spec = tabs. newTabSpec ("Tag2 ");
Spec. setContent (R. id. c92_tab2 );
Spec. setIndicator ("Button ");
Tabs. addTab (spec );
// Step 3: You can use setCurrentTab (index) to specify the displayed page, which starts from 0.
Tabs.Setcurrenttab(1 );
}
Example 2: Set tags
In addition to the text given above, tag settings can also provide widgets, such:
Button B = new Button (this );
B. setText ("tag ");
Spec. setIndicator (B );
This is also work, of course, it is boring to set the button, usually we want to be an image, as shown in the following three figures:
1) We add an icon to the tag, such as the left image. This image exists in the drawable/directory:
Resources res = getResources ();
Spec. setIndicator ("Button", res. getDrawable (R. drawable. star_logo ));
2) We create an image on the tab. If you select this page as an icon, select another one if you do not select it. As shown in the figure above and in the right. We have two image files in the drawable directory. Session notes on Android (6): View in xml and widget. We create an XML file, description:
<? Xml version = "1.0" encoding = "UTF-8"?>
<SelectorXmlns: android = "http://schemas.android.com/apk/res/android">
<! -- This is executed sequentially. If the status is selected, it is the android_focused icon. If it is not the next step, use the android_normal icon. -->
<ItemAndroid: drawable = "@ drawable/android_focused"
Android: state_selected= "True"/>
<ItemAndroid: drawable = "@ drawable/android_normal"
/>
</Selector>
The name of this file is chapter_9_tutorial3_ic.xml. To facilitate management, this time we did not place it in the layout directory, but in the drawable directory. The source code is as follows:
Resources res = getResources ();
Spec. setIndicator ("Button", res. getDrawable (R. drawable. chapter_9_tutorial3_ic ));
Example 3: dynamically Add a tab
In some cases, we need to dynamically Add a Tab page, which can be implemented through TabContentFactory. In the following example, click the button in the Tab to add a new tab page.
Button button = (Button) findViewById (R. id. c92_tabhost );
Button. setOnClickListener (new View. OnClickListener (){
Public void onClick (View arg0 ){
TabHost. TabSpec spec = tabs.Newtabspec("Tag1 ");
Spec. setContent (new TAbhost. tabcontentfactory(){
/* CreateTabContent will return the View. Here we simply use a simulated clock */
Public ViewCreatetabcontent(String tag ){
Return new AnalogClock (Chapter9Test3. this );
}
});
Spec. setIndicator ("Clock ");
Tabs. addTab (spec );
}
});
Example 4: add an activity to the tab
In the Tutorial of the Android SDK, an example is provided. The setContent (Intent intent) method is used to pass an Activity in the content. This example takes a long time because an error is reported at the beginning of the operation. Therefore, pay attention to the following two issues during writing:
- Before using setcontent (intent), you must perform setup (localactivitymanager activitygroup) on tabhost, because activitygroup is required when acivity is called. If tabactivity is inherited, tabacitivty automatically completes tabactivity, since we do not know about localactivitymanager at present, we can simply inherit tabactivity
- The activity cannot be an internal class. The activity must be registered in androidmanifest.
For point 1st, our XML file is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<TabhostXmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ Android: ID/tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Linearlayout Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: padding = "5dp">
<TabWidget android: id = "@ android: id/tabs"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<FrameLayout android: id = "@ android: id/tabcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: padding = "5dp">
</Framelayout>
</Linearlayout>
</Tabhost>
For the 2nd requirement, we can directly use the previously created Activity. The Code is as follows:
Public class chapter9tutori_3
Extends tabactivity{
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. chapter_9_tutori_3 );
Tabhost tabs = gettabhost ();
Tabhost. tabspec spec = NULL;
// Import the activity through intent. For intent, this class cannot be an internal class. This class must be registered in androidmanifest.
Intent intent = new intent ();
Intent. setclass (this, chapter9tutorial. Class );
Spec = tabs. newtabspec ("tag1 ");
Spec. setindicator ("intent ");
Spec. setcontent (intent );
Tabs. addtab (SPEC );
......
}
}
Related Links: My Andriod development articles