首页 > 代码库 > Android中动态添加tab

Android中动态添加tab

来源过于啰嗦,这里只有简化后的。

  转载请注明出处  http://www.cnblogs.com/zaiyuzhong/p/add-tab-dynamic-in-android.html

 

建立对应的布局配置:/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?><TabHost    android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:id="@+id/tabHost"            xmlns:android="http://schemas.android.com/apk/res/android">     <TabWidget        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@android:id/tabs"/>     <FrameLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:id="@android:id/tabcontent">     </FrameLayout> </TabHost>
技术分享
 1 import android.view.Menu; 2 import android.view.View; 3   4 import android.widget.AnalogClock; 5   6 import android.widget.TabHost; 7 import android.widget.TabHost.TabSpec; 8 import android.widget.TextView; 9  10  11 public class MainActivity extends FragmentActivity {12  13     @Override14     protected void onCreate(Bundle savedInstanceState) {15         super.onCreate(savedInstanceState);16         setContentView(R.layout.activity_main);17          18         TabHost tabHost=(TabHost)findViewById(R.id.tabHost);19         tabHost.setup();20  21         TabSpec spec1=tabHost.newTabSpec("Tab1");22         spec1.setContent(new TabHost.TabContentFactory() {23             public View createTabContent(String tag) {24                 TextView txtView = new TextView(MainActivity.this);25                 txtView.setText("Tab Text in createTabContent");26                 return txtView;27             }28         });29         spec1.setIndicator("Tab Text for setIndicator");30          31  32         TabSpec spec2=tabHost.newTabSpec("Tab2");33         spec2.setIndicator("Tab Clock");34         spec2.setContent(new TabHost.TabContentFactory() {35             public View createTabContent(String tag) {36                 return(new AnalogClock(MainActivity.this));37             }38         });39         spec2.setIndicator("Clock");40          41         tabHost.addTab(spec1);42         tabHost.addTab(spec2);43     }
代码

Android中动态添加tab