If you want to place a lot of controls on the screen, you may not fit a screen, in addition to using the scrolling view, you can also use the Label control to display the screen, when you click the Label control's different labels, the current label content is displayed, A tag in an Android system can be either a view or an activity. Tabhost is the core class of the label control and is also a collection of labels, each of which is a Tabhost.tabspec object. Add multiple Tabhost.tabspec objects by using the AddTab method of the Tabhost class. Here is the real column: in the Main.xml file:
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent" >5<TextView6Android:id= "@+id/textview1"7Android:layout_width= "Fill_parent"8android:layout_height= "Fill_parent"9android:background= "#FF0000"/>Ten<TextView OneAndroid:id= "@+id/textview2" AAndroid:layout_width= "Fill_parent" -android:layout_height= "Fill_parent" -android:background= "#00FF00"/> the<TextView -Android:id= "@+id/textview3" -Android:layout_width= "Fill_parent" -android:layout_height= "Fill_parent" +android:background= "#0000FF"/> -</RelativeLayout>
In the. java file:
1 Public classMainactivityextendstabactivity {//note, here inherit tabactivity, detailed description of the handbook2 3 PrivateTabhost host;4@SuppressWarnings ("Deprecation")5 @Override6 protected voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 //Setcontentview (r.layout.main);9 TenHost =gettabhost (); OneLayoutinflater.from ( This). Inflate (R.layout.main,host.gettabcontentview (),true); AHost.addtab (Host.newtabspec ("Tab1"). Setindicator ("Red", Getresources (). Getdrawable (Android. R.drawable.ic_input_add)). SetContent (R.id.textview1)); -Host.addtab (Host.newtabspec ("TaB2"). Setindicator ("green"), Getresources (). Getdrawable (Android. R.drawable.ic_input_delete)). SetContent (R.ID.TEXTVIEW2)); -Host.addtab (Host.newtabspec ("Tab3"). Setindicator ("Blue", Getresources (). Getdrawable (Android. R.drawable.ic_input_get)). SetContent (R.ID.TEXTVIEW3)); theHost.setontabchangedlistener (NewOntabchangelistener () { - - @Override - Public voidontabchanged (String tabId) { + //TODO Auto-generated method stubs -Toast.maketext (mainactivity. This, "TabId" +tabId, Toast.length_long). Show (); + } A }); at - } - - @Override - Public BooleanOncreateoptionsmenu (Menu menu) { - //inflate the menu; This adds items to the action bar if it is present. in getmenuinflater (). Inflate (R.menu.main, menu); - return true; to } + -}
Operation Result:
The most complete android tab and Tabhost explained in history Http://www.eoeandroid.com/thread-1035-1-1.html |
Tabhost Label controls