public void initTabLayout() { TabHost tabHost = new TabHost( this ,null);//后面这个null一定要加,否则在addTab时会报资源找不到的错误 LinearLayout lineLayout = new LinearLayout( this ); lineLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); lineLayout.setOrientation( LinearLayout.VERTICAL ); lineLayout.setGravity( Gravity.TOP ); TabWidget tabWidget = new TabWidget( this ); tabWidget.setId( android.R.id.tabs ); //TabHost必须有一个ID为R.id.tabs的TabWidget,内部代码是根据此ID来打出TabWidget, //详看TabHost的无参setup方法 tabWidget.setLayoutParams( new LinearLayout.LayoutParams( -1, -2) ); FrameLayout frameLayout = new FrameLayout( this ); frameLayout.setId( android.R.id.tabcontent );//TabHost必须有一个ID为R.id.tabContent的FrameLayout frameLayout.setLayoutParams( new LinearLayout.LayoutParams( -1, -2) ); Button b1 = new Button( this ); b1.setId(123); b1.setText(" tab button1 "); Button b2 = new Button( this ); b2.setId(456); b2.setText(" tab button2 "); frameLayout.addView( b1 ); frameLayout.addView( b2 ); lineLayout.addView( tabWidget ); lineLayout.addView( frameLayout ); tabHost.addView( lineLayout ); // Call setup() before adding tabs if loading TabHost using findViewById(). // You do not need to call setup() after getTabHost() //在添加tabs之前要调用,当然前提是TabHost不是通过getTabHost获取,也就是说不是通过TabActivity实现 tabHost.setup(); //不能直接new TabSpec TabSpec deviceTab = tabHost.newTabSpec("Device"); deviceTab.setIndicator( "Device Tab" ); deviceTab.setContent( b1.getId() ); TabSpec softTab = tabHost.newTabSpec("Soft"); softTab.setIndicator( "Soft Tab" ); /** * 如果用TabHost tabHost = new TabHost( this );获取tabHost会报这种错 * Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0 * at android.content.res.Resources.getValue(Resources.java:1013) * at android.content.res.Resources.loadXmlResourceParser(Resources.java:2098) * at android.content.res.Resources.getLayout(Resources.java:852) * at android.view.LayoutInflater.inflate(LayoutInflater.java:394) * at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:543) * at android.widget.TabHost.addTab(TabHost.java:223) * 下面来分析原因: * TabHost 541行: * LayoutInflater inflater = * (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); * View tabIndicator = inflater.inflate(mTabLayoutId, * mTabWidget, // tab widget is the parent * false); // no inflate params * inflate会去查找mTabLayoutId定义的资源文件,那么mTabLayoutId又是怎么来的? * * TabHost有两个构造器,TabHost( Context contex )里面是没有初始化mTabLayoutId,这样对象加载后它默认为0 * 再看TabHost 82行(再第二个构造器TabHost( Context context, AttributeSet attrs)中有对它进行初始化): * mTabLayoutId = a.getResourceId(R.styleable.TabWidget_tabLayout, 0); * a.recycle(); * if (mTabLayoutId == 0) { * // In case the tabWidgetStyle does not inherit from Widget.TabWidget and tabLayout is * // not defined. * mTabLayoutId = R.layout.tab_indicator_holo; * } */ softTab.setContent( b2.getId() ); tabHost.addTab( deviceTab ); tabHost.addTab( softTab ); tabHost.setCurrentTab( 0 ); linearLayout.addView( tabHost ); }