Android之TabActivity

 TabActivity可实现分页式布局效果

详细步骤如下:

1)定义一个activity继承TabActivity(如MainActivity)

2)创建TabHst分页菜单对象

//替代了setContentView(R.layout.main);
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView());

当然你也可以使用setContentView(R.layout.main);如此则建议在布局文件main.xml中定义TabHost

<TabHost 
	android:id="@android:id/tabhost" 
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout 
		android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
		<TabWidget 
			android:id="@android:id/tabs"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:layout_margin="10dip"/>
		<FrameLayout 
			android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
   	</LinearLayout>
</TabHost>

3)实例声明TaSpec对象,然后添加到TabHost中

private void initTab(){
	/**
	 * 定义TabSpec
	 * 指定标识tag
	 */
	TabSpec tsA=tabHost.newTabSpec("A");
	/**
	 * 设置标题 
	 * 不仅是文字也可是是context对象
	 */
	tsA.setIndicator("基础");
	/**
	 * 设置当前分页的内容 不仅是viewId也可以是intent,如:
	 * Intent intent=new Intent(TabMainActivity.this,DishListActivity.class);
	 * spec.setContent(intent);
	 * 则点击该分页按钮将弹出DishListActivity界面
	 */
	tsA.setContent(R.id.btn);
	/**
	 * 添加到当前tabHost中
	 * 注意:
	 * 	1)tabHost可以通过this.getTabHost()获取
	 * 	2)上述的设置分页内容需在执行LayoutInflater操作后添加分页内容
	 */
	tabHost.addTab(tsA);
	
	tsB=tabHost.newTabSpec("B");
	tsB.setIndicator("进阶");
	tsB.setContent(R.id.edt);
	tabHost.addTab(tsB);
	
	tsC=tabHost.newTabSpec("C");
	tsC.setIndicator("高级");
	tsC.setContent(R.id.txtA);
	tabHost.addTab(tsC);
}



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值