TabHost注意事项

来源:互联网 发布:unity3d中播放视频 编辑:程序博客网 时间:2024/04/29 17:21

今天使用TabHost控件,竟然半天没弄出来,一直报NULL;

原来是少了

tabHost.setup();

API说明如下:

 void android.widget.TabHost.setup()public void setup () Added in API level 1Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in TabActivity. Example:mTabHost = (TabHost)findViewById(R.id.tabhost);mTabHost.setup();mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");

使用TabActivity getTabHost()方法,就不用调用 setup() 方法。

而使用findViewById 则需要调用。


host=(TabHost) this.findViewById(android.R.id.tabhost);//获得 android:id="@android:id/tabhost" 控件idhost.setup();host.addTab(host.newTabSpec("TAB1").setContent(R.id.tab1).setIndicator("TAB1"));host.addTab(host.newTabSpec("TAB2").setContent(R.id.tab2).setIndicator("TAB1"));host.addTab(host.newTabSpec("TAB3").setContent(R.id.tab3).setIndicator("TAB1"));


原创粉丝点击