Android中動態添加tab

來源:互聯網
上載者:User

標籤:super   show   分享   for   ide   txt   tin   color   target   

來源過於囉嗦,這裡只有簡化後的。

  轉載請註明出處  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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.