Android中使用TabHost 可以在一个屏幕间进行不同版面的切换:
在Android中要实现Tab切换页面的效果,大致有两种方法:一是利用layout下的xml配置来实现tab的布局,二是动态加载tabHost进行布局。

1、利用xml文件配置,引用xml的activity不用继承于TabActivity,通过findViewById方法获取TabHost,

具体的xml见下:

<?xml version="1.0" encoding="utf-8"?><TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent">                <!-- this is tabs content --><FrameLayout android:id="@android:id/tabcontent"  android:layout_width="fill_parent" android:layout_height="wrap_content"android:paddingBottom="62px"><!-- tab1 content --><LinearLayout android:id="@+id/content1"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical">。。。。。。。。。。。。</LinearLayout>                        <!-- tab2 content --><LinearLayout android:id="@+id/content2"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical">        。。。。。。。。。。。</LinearLayout></FrameLayout><RelativeLayout                              android:layout_width="fill_parent"                              android:layout_height="fill_parent">                             <TabWidget                  android:id="@android:id/tabs"                  android:layout_alignParentBottom="true"                  android:layout_width="fill_parent"                  android:layout_height="wrap_content" />             </RelativeLayout> </LinearLayout></TabHost>

xml中TabHost,FrameLayout,TabWidget 元素都要引用系统的id,若改用其他id会抛异常的,绿色的部分可以自定义

Activity部分代码:

setContentView(R.layout.***); //设置上面的xml文件
                TabHost tabhost = (TabHost) findViewById(R.id.tabhost);tabhost.setup();   // 初始化TabHost容器// tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", null).setContent(R.id.content1));TabHost.TabSpec tab_1 = tabhost.newTabSpec("tab_1"); // 设置一个tabtab_1.setContent(R.id.content1); // 设置tab里面的布局tab_1.setIndicator("按地名查询"); // 设置tab的标题和图表tabhost.addTab(tab_1); // 在tabhost上添加新的tab// tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2", null).setContent(R.id.content2));TabHost.TabSpec tab_2 = tabhost.newTabSpec("tab_2");tab_2.setContent(R.id.content2);tab_2.setIndicator("按起点终点查询");tabhost.addTab(tab_2);// 设置默认显示第几个tabtabhost.setCurrentTab(0);

2、继承TabActivity 类,,Activity中使用getTabHost()获得TabHost,使用android的自己内部定义好的.xml资源文件作容器文件,进行动态布局;

tab_search.xml,用来布局tabHost下的content

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:id="@+id/tab_hs_id">        <TextView android:id="@+id/tab_search_tv"    android:layout_width="fill_parent"    android:layout_height="fill_parent"/>    </FrameLayout>

Activity中的代码:

tabHost = getTabHost();
                LayoutInflater inflater_tab3 = LayoutInflater.from(this);inflater_tab3.inflate(R.layout.tab_search, tabHost.getTabContentView());                /* * R.layout.tab_search已被LayoutInflater注册,所以这个content可以通过findViewById获得其对象 */TabHost.TabSpec tabSpec_near = tabHost.newTabSpec("nearest");tabSpec_near.setIndicator("Nearest", null);tabSpec_near.setContent(R.id.tab_hs_id); // 绑定一个新的LayouttabHost.addTab(tabSpec_near);  //添加一个tab       setContentView(tabHost);  //设置avtivity的布局为tabHost


更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. Android(安卓)沉浸式状态栏完美解决方案
  4. Android(安卓)Dependencies
  5. Android(安卓)Manifest文件
  6. 安装APK文件到Android模拟器
  7. Android(安卓)引用外部字体
  8. Android动态加载技术初探
  9. 关于那些Android中不常用的设置属性

随机推荐

  1. Google Developing for Android(安卓)三
  2. Android(安卓)Framework 基础理解
  3. Android(安卓)EditView属性
  4. 转载 android 利用ksoap2方式连接webserv
  5. android UI小结(二)
  6. 初探Android
  7. 【Android(安卓)系统开发】 编译 Android
  8. android开发初学建议
  9. Android(安卓)开发小技巧
  10. Android高斯模糊、高斯平滑(Gaussian Blur