android不用xml实现Tab效果(TabHost学习)

 

 

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 );
	}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值