In the Phone screen, tab is also more commonly used, usually with the list, such as our mobile phone records. The following is the structure of the tag.
Tabhost is the entire tab container, consisting of two parts, Tabwidget and Framelayout. Tabwidget is the label for each tab, and Framelayout is the tab content.
- If we use extends Tabacitivty, as if listactivity,tabhost must be set to @android:id/tabhost
- Tabwidget must be set Android:id to @android:id/tabs
- Framelayout need to set Android:id to @android:id/tabcontent
Example one: 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: Get the Tabhost object and initialize Setup ()
TabhostTabs = (tabhost) Findviewbyid (r.id.c92_tabhost);
Tabs.setup ();
Step 2: Increase tab page by Tabhost.tabspec, add content via SetContent (), Add tab of page by Setindicator
/* (1) Add page 1th */
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: The displayed page can be specified by Setcurrenttab (index) and calculated starting at 0.
Tabs.Setcurrenttab(1);
}
Example two: Setting the label
Label settings In addition to the above text, you can also give the widget, for example:
Button B = New button (this);
B.settext ("label");
Spec.setindicator (b);
This is also work, of course, set button is very boring, usually we want to be a picture, such as the following three pictures:
1) We add an icon to the label, as shown on the left. This image exists in the drawable/directory:
Resources res = getresources ();
Spec.setindicator ("button", Res.getdrawable (R.drawable.star_logo));
2) We have a picture on the label home, when the user chooses the page is for an icon, not selected when the other icon. As shown in the picture above and on the right. We have two picture files related to the Drawable catalog project. Meetings in Android learning Note (vi): XML and Widgets in view, we create an XML file that describes:
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--This is done sequentially, if the status is selected, then the android_focused icon, if not next, takes the Android_normal icon--
<item android:drawable= "@drawable/android_focused"
android:state_selected= "true"/>
<item android:drawable= "@drawable/android_normal"/>
</selector>
This file we named Chapter_9_tutorial3_ic.xml, in order to facilitate management, this time we did not put it in the layout directory, but the way 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 three: Dynamically add tab pages
In some cases, we need to dynamically add tab pages, which can be implemented by Tabcontentfactory. In the following example, we click the tab button and a tab page is added.
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 to view, here we simply use an analog clock */
Public Viewcreatetabcontent(String tag) {
return new AnalogClock (chapter9test3.this);
}
});
Spec.setindicator ("Clock");
Tabs.addtab (spec);
}
});
Example four: Adding an Activity to the tab page
An example is given in the tutorial of the Android SDK, which uses setcontent (Intent Intent) to pass an activity in the content. This example cost me a lot of time, because when I start running, I get an error. So when you write, pay attention to the following two questions:
- Before using SetContent (Intent Intent), the tabhost must be setup (Localactivitymanager Activitygroup) because acivity is required when Activitygroup , if we inherit tabactivity, it will be done automatically by tabacitivty, since we are currently not aware of Localactivitymanager, simply can inherit directly tabactivity
- Activity cannot be an internal class, activity must be registered in Androidmanifest
For the 1th, 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 activity we created before, the code is as follows
public class Chapter9tutorial3extends Tabactivity{
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.CHAPTER_9_TUTORIAL3);
Tabhost tabs = Gettabhost ();
Tabhost.tabspec spec = null;
By intent, the activity is imported, for intent, this class cannot be an inner class, the class must be registered in Androidmanifest
Intent Intent = new Intent ();
Intent.setclass (this, chapter9tutorial2.class);
Spec = Tabs.newtabspec ("Tag1");
Spec.setindicator ("Intent");
spec.setcontent (intent);
Tabs.addtab (spec);
. .. ...
}
}
RELATED links: My Andriod development related articles
Android Learning Note (22): Use of multi-page display-tag