Today, let's talk about how to put tabhost in tabhost.
Let's take a look at the effect.
Tabhost Layer
Two-layer tabhost (internal tabhots on top), as shown in the right figure above.
Two-layer tabhost (the inner tabhots is below)
Let's talk about the code below. There are three Java classes and three XML layout files.
Take a look at the main screen:
Main. xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/default_bg"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> </TabHost>
Is the regular tabhost layout.
Entry class:
Doubletabhost. Java
Package COM. yfz; import android. app. tabactivity; import android. content. intent; import android. OS. bundle; import android. widget. tabhost;/*** this class inherits tabactivity * @ author administrator **/public class doubletabhost extends tabactivity {/* Note: * The tabhost and layout files must contain * tabhost, tabwidget, and framelayout *. If you inherit tabactivity and use the gettabhost () method to obtain tabhost *, the IDs of the three must be android. r. id. tabhost, android. r. id. tabs, android. r. id. if tabcontent ** inherits the activity, you can use findviewbyid to obtain the three components. At this time, the ID can be customized * // ** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); tabhost mtabhost = gettabhost (); mtabhost. addtab (mtabhost. newtabspec ("Twitter "). setindicator ("Twitter", getresources (). getdrawable (Android. r. drawable. arrow_down_float )). setcontent (new intent (this, subtab. class); mtabhost. addtab (mtabhost. newtabspec ("Facebook "). setindicator ("Facebook", getresources (). getdrawable (Android. r. drawable. arrow_down_float )). setcontent (new intent (this, normalactivity. class); mtabhost. setcurrenttab (0 );}}
Tabhost and layout files must contain tabhost, tabwidget, and framelayout.
If the class that loads the tabhost image inherits tabactivity and you want to use the gettabhost () method to obtain tabhost, The gettabwidget () method to obtain tabwidget,
The IDS of tabhost, tabwidget, and framelayout must be Android. R. Id. tabhost, Android. R. Id. tabs, and Android. R. Id. tabcontent.
Otherwise, an exception occurs during the running process. The error is as follows:
Tabhost ID error:
ERROR/AndroidRuntime(8301): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
Tabwidget ID error:
ERROR/AndroidRuntime(8354): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
Framelayout ID error:
ERROR/AndroidRuntime(8404): Caused by: java.lang.RuntimeException: Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'
Sub tabhost page:
Subtab. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Tabhost xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/mytabhost" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: background = "@ drawable/default_bg"> <linearlayout Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Pay attention to the location of the framelayout/tabwidget tag --> <framelayout Android: Id = "@ Android: ID/tabcontent" Android: layout_weight = "1" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textviewandroid: Id = "@ + ID/widget59" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "What are you thinking? "Android: layout_alignparenttop =" true "Android: layout_centerhorizontal =" true "> </textview> <textviewandroid: Id =" @ + ID/widget60 "Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "I'm thinking about Android" Android: layout_alignparenttop = "true" Android: layout_alignparentright = "true"> </textview> </framelayout> <tabwidget Android: Id = "@ Android: ID/tabs" Android: layout_alignparentbottom = "true" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> </linearlayout> </tabhost>
Sub-tabhost page loading class:
Subtab. Java
Package COM. yfz; import android. app. activity; import android. OS. bundle; import android. widget. tabhost; import android. widget. tabwidget; import android. widget. textview;/***** @ author administrator **/public class subtab extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. subtab); // The following three codes. Note the order tabhost mtabhost = (tabhost) findviewbyid (R. id. mytabhost); mtabhost. setup (); tabwidget = mtabhost. gettabwidget (); mtabhost. addtab (mtabhost. newtabspec ("Suzhou "). setindicator ("Suzhou "). setcontent (R. id. widget59); mtabhost. addtab (mtabhost. newtabspec ("Shanghai "). setindicator ("Shanghai "). setcontent (R. id. widget60); mtabhost. addtab (mtabhost. newtabspec ("Tianjin "). setindicator ("Tianjin "). setcontent (R. id. widget60); mtabhost. addtab (mtabhost. newtabspec ("Beijing "). setindicator ("Beijing "). setcontent (R. id. widget60); mtabhost. setcurrenttab (0); int Height = 30; // int width = 45; For (INT I = 0; I <tabwidget. getchildcount (); I ++) {/** sets the height and width. Since the width is set to fill_parent, it is ineffective here */tabwidget. getchildat (I ). getlayoutparams (). height = height; // tabwidget. getchildat (I ). getlayoutparams (). width = width;/** set the color of the title text in the tab. Otherwise, the color is black by default */FINAL textview TV = (textview) tabwidget. getchildat (I ). findviewbyid (Android. r. id. title); TV. settextcolor (this. getresources (). getcolorstatelist (Android. r. color. white ));}}}
If you load the tabhost image from the activity, you can use findviewbyid to obtain the three components. In this case, the ID can be customized.
Note that you must call the setup method to load tabhost.
By the way, do not forget to define activity ~~ in androidmanifest. xml ~~ Haha!
There are so many points ~ That's it.
Source program: http://download.csdn.net/source/3037680
Bug. Thanks to simayilong and gz116 for proposing these two typical questions.
Problem 1: The activity cannot be opened in the sub tabhost.
java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
Solution 1:
1. The class that initializes tabhost (in this example, it is the subtab class) and inherits the activitygroup instead of the activity
2. Change tabhost. Setup () to tabhost. Setup (this. getlocalactivitymanager ());
For details, refer:
Http://stackoverflow.com/questions/3272500/android-exception-did-you-forget-to-call-public-void-setup-localactivitymanage
Http://hi.baidu.com/ljlkings/blog/item/47f1afdbdcd27de638012f76.html
Solution 2:
You can also inherit tabactivity from the subtab class, and change the tabhost ID in the layout file to @ Android: ID/tabhost.
Because tabactivity also inherits from activitgroup.
Both methods can be used.
Problem 2: After tabhost is nested, the dialog cannot be displayed.
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@43e57e20 is not valid; is your activity running?
Solution:
Alertdialog. Builder (XXX. This) => alertdialog. Builder (XXX. This. getparent ())
References:
Nested tabhost (put tabhost in tabhost, similar to level-2 Directory and level-2 tree)
[Android advanced] nested tabhost (put tabhost in tabhost, similar to Level 2 Directory and level 2 tree)