The source is too verbose, and here it is only simplified.
Reprint please specify the source http://www.cnblogs.com/zaiyuzhong/p/add-tab-dynamic-in-android.html
Establish the corresponding layout configuration:/res/layout/activity_main.xml
<?XML version= "1.0" encoding= "Utf-8"?><TabhostAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:id= "@+id/tabhost"xmlns:android= "Http://schemas.android.com/apk/res/android"> <TabwidgetAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:id= "@android: Id/tabs"/> <FramelayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:id= "@android: Id/tabcontent"> </Framelayout> </Tabhost>
1 ImportAndroid.view.Menu;2 ImportAndroid.view.View;3 4 ImportAndroid.widget.AnalogClock;5 6 ImportAndroid.widget.TabHost;7 ImportAndroid.widget.TabHost.TabSpec;8 ImportAndroid.widget.TextView;9 Ten One Public classMainactivityextendsfragmentactivity { A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - -Tabhost tabhost=(tabhost) Findviewbyid (r.id.tabhost); + Tabhost.setup (); - +Tabspec Spec1=tabhost.newtabspec ("Tab1"); ASpec1.setcontent (Newtabhost.tabcontentfactory () { at PublicView createtabcontent (String tag) { -TextView Txtview =NewTextView (mainactivity. This); -Txtview.settext ("tab Text in Createtabcontent"); - returnTxtview; - } - }); inSpec1.setindicator ("tab Text for Setindicator"); - to +Tabspec Spec2=tabhost.newtabspec ("Tab2"); -Spec2.setindicator ("tab Clock"); theSpec2.setcontent (Newtabhost.tabcontentfactory () { * PublicView createtabcontent (String tag) { $ return(NewAnalogClock (mainactivity. This));Panax Notoginseng } - }); theSpec2.setindicator ("Clock"); + A Tabhost.addtab (SPEC1); the Tabhost.addtab (SPEC2); +}
Code
Dynamically add tab in Android