TabhostThe main feature is that you can display the contents of multiple groups of tabs in one window, and each tab bar in the Android system is called a tab, and the container containing the multiple tab bars is called the Tabhost,tabhost class's inheritance structure as follows:Java.lang.Object?Android.view.View?Android.view.ViewGroup?Android.widget.FrameLayout ?Android.widget.TabHost
the common methods are as follows
| 1 |
Public Tabhost (Context context) |
Structure |
Creating a Tabhost Class object |
| 2 |
public void AddTab (Tabhost.tabspec tabspec) |
Ordinary |
Add a tab |
| 3 |
Public Tabhost.tabspec Newtabspec (String tag) |
Ordinary |
Create a Tabhost.tabspec object |
| 4 |
Public View Getcurrentview () |
Ordinary |
Gets the current View object |
| 5 |
public void Setup () |
Ordinary |
Creating Tabhost Objects |
| 6 |
public void Setcurrenttab (int index) |
Ordinary |
Sets the tab number that is currently displayed |
| 7 |
public void Setcurrenttabbytag (String tag) |
Ordinary |
Sets the tab name that is currently displayed |
| 8 |
Public Framelayout Gettabcontentview () |
Ordinary |
Return to label container |
| 9 |
public void Setontabchangedlistener (Tabhost.ontabchangelistener l) |
Ordinary |
Trigger when setting label changes |
Two ways to achieve tabhost
Way One:directly let aActivityProgram Inheritancetabactivityclass;Way Two:UseFindviewbyid ()method to obtainTaghostcomponents, and make several configurations;
The first way to have a class inherit tabactivity
XML file configuration requires nesting in an XML file to use layouts to display different content in different tabs
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <linearlayout android:layout_margintop= "50dp" android:id= "@+id/login" android:layout_width = "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <tablerow Android:id= "@+id/tablerow1" android:layout_width= "Match_parent" android:layout_height= "W Rap_content "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:text=" account "/> <ed Ittext android:id= "@+id/edittext1" android:layout_width= "Wrap_content" Androi d:layout_height= "Wrap_content" android:ems= "android:textsize=" 25sp "/> </TableRow> <tablerow Android:id= "@+id/tablerow2" android:layout_width= "match_parent" android:layout_height= "Wrap_c Ontent "> <textview android:id=" @+id/textview2 "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:text=" password "/> <edittex T android:id= "@+id/edittext2" android:layout_width= "Wrap_content" Android:lay out_height= "Wrap_content" android:ems= "android:textsize=" 25sp "/> </tablero w> <tablerow android:id= "@+id/tablerow3" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > <button android:id= "@+id/button1" a Ndroid:layout_width= "WraP_content "android:layout_height=" Wrap_content "android:layout_weight=" 1 "and roid:text= "Cancel"/> <button android:id= "@+id/button2" Android:layout_width= "W Rap_content "android:layout_height=" Wrap_content "android:layout_weight=" 1 "a ndroid:text= "Login"/> </TableRow> </LinearLayout> <linearlayout android:layout_margint op= "50DP" android:id= "@+id/image" android:layout_width= "match_parent" android:layout_height= "Match_pa Rent "android:orientation=" vertical "> <imageview android:layout_width=" wrap_content " android:layout_height= "Wrap_content" android:src= "@drawable/a2"/> </LinearLayout> <lin Earlayout android:layout_margintop= "50DP" android:id= "@+id/timer" android:layout_width= "Match_parent" Android:layout_height= "match_parent" android:orientation= "vertical" > <timepicker android:layout_width= "Mat Ch_parent "android:layout_height=" wrap_content "/> </LinearLayout> <linearlayout android: layout_margintop= "50DP" android:id= "@+id/timer1" android:layout_width= "Match_parent" Android:layout_h eight= "match_parent" android:orientation= "vertical" > <timepicker android:layout_width= "Mat Ch_parent "android:layout_height=" wrap_content "/> </LinearLayout> <linearlayout Android oid:layout_margintop= "50DP" android:id= "@+id/timer2" android:layout_width= "Match_parent" Android:layo ut_height= "match_parent" android:orientation= "vertical" > <timepicker android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> </LinearLayout></LinearLayout>
Java File Settings
Package Com.example.tabhost;import Android.app.tabactivity;import Android.os.bundle;import Android.view.layoutinflater;import Android.widget.tabhost;import Android.widget.tabhost.tabspec;public Class Mainactivity extends Tabactivity {private tabhost tabhost;//initialize tabhost component private int reslayout[] = {R.id.login, r.id.imag E, R.id.timer, r.id.timer1,r.id.timer2};//set the corresponding amount XML file private int Images[]={r.drawable.cart,r.drawable.cloud, r.drawable.comment,r.drawable.gear,r.drawable.joystick};//set the caption file displayed @suppresswarnings ("deprecation") @ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); this.tabhost = Super.gettabhost ();//Instantiate Tabhost component//Get Layoutinflater Object Layoutinflater.from (this). Inflate (r.layout.linearlayout,/ /Make Layout This.tabHost.getTabContentView (),//Make label add container true); for (int i = 0; i < reslayout.length; i++) {Tabspec mytab = tab Host.newtabspec ("tab" + i);//define Tabspecmytab.setindicator (Null,getresources (). getdrawable (Images[i]));// Set the label mytab.setcontent (This.reslayout[i]);//Set the displayed component This.tabHost.addTab (Mytab);}}
As follows
Use configuration file settings. This method is more common, can be said tabhost at the bottom
But the layout of the file is more complex, you can refer to the example of specific learning
XML file
<?xml version= "1.0" encoding= "Utf-8"? ><tabhost xmlns:android= "Http://schemas.android.com/apk/res/android "Android:id=" @+id/tabhost "android:orientation=" vertical "android:layout_width=" fill_parent "Android:layout_ height= "Fill_parent" ><relativelayout 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 "android:layout_alignparentbottom=" true "/><framelayout Android: Id= "@android: Id/tabcontent" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" > < LinearLayout android:id= "@+id/login" android:layout_width= "Match_parent" Android:layout_heigh t= "match_parent" android:orientation= "vertical" > <tablerow android:id= "@+id/tablerow1" Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > <textview android:id= "@+id/textview1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "account"/> <edittext Android:id= "@+id/edittext1" android:layout_width= "wrap_content" android:layout_height= "Wra P_content "android:ems=" android:textsize= "25sp"/> </TableRow> &l T TableRow android:id= "@+id/tablerow2" android:layout_width= "Match_parent" Android:layout_h eight= "Wrap_content" > <textview android:id= "@+id/textview2" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:text= "password"/> <edittext android:id= "@+id/edittext2" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:ems= "android:textsize=" 25sp "/> </TableRow> <tablerow android:id= "@+id/tablerow3" android:layout_width= "Match_parent" Android: layout_height= "Wrap_content" > <button android:id= "@+id/button1" Android:la Yout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Cancel"/> <button android:id= "@+id/button2" Android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Login"/> </TableRow> </LinearLayout> <linearlayout android : id= "@+id/image" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:ori entation= "Vertical" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/a2"/> </LinearLayout> <linearlayout android:id= "@+id/timer" Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <timepicker android:layout_width= "match_parent" android:layout_height= "Wrap_content"/> ; </LinearLayout> <linearlayout android:id= "@+id/timer1" android:layout_width= "Match_parent" a ndroid:layout_height= "match_parent" android:orientation= "vertical" > <timepicker android:la Yout_width= "Match_parent" android:layout_height= "wrap_content"/> </LinearLayout> <linearlay Out android:id= "@+id/timer2" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:orientation= "vertical" > <timepicker android:layout_width= "match_parent" android:layout_he ight= "Wrap_content"/> </linearlayout></framelayout></relativelayout ></TabHost>
Java file Configuration
Package Com.example.tabhost;import Android.os.bundle;import Android.app.activity;import android.widget.TabHost; Import Android.widget.tabhost.tabspec;public class Mainactivity extends Activity {//Direct inheritance activityprivate tabhost mytabhost;//definition tabhostprivate int[] layres = {r.id.login, r.id.image, R.id.timer, R.id.timer1, R.id.timer2};//define inline layout management Device Idprivate int Images[]={r.drawable.cart,r.drawable.cloud,r.drawable.comment,r.drawable.gear,r.drawable.joystick };//title picture data @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Super.setcontentview (R.layout.activity_main);//Call the default layout manager This.mytabhost = (tabhost) Super.findviewbyid ( R.id.tabhost); Get Tabhost object This.myTabHost.setup ();//Create Tabhost object for (int x = 0; x < this.layRes.length; + +) {//loop out all layout tags tabspec Mytab = Mytabhost.newtabspec ("tab" + x);//define Tabspecmytab.setindicator (Null,getresources (). getdrawable (Images[x])) ;//Set Label text Mytab.setcontent (This.layres[x]);//Set the displayed component This.myTabHost.addTab (mytab);//Add Tag}this.mytabhost.setcurrenttab (0);//Set Start Index}}
Using the Tabhost component to set tab switching and intent is more commonly used in development, and is the foundation of the app development framework.
Next section forecast: Menu
Learn android<tabhost label components from scratch. 29 .>