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);
}