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系统权限和root权限
  4. Android小技术知识(多用于面试)
  5. android 五大布局文件
  6. android APN的打开与关闭
  7. Android(安卓)VR Player(全景视频播放器) [8]:MediaPlayer+SurfaceV
  8. android根据uri或文件绝对路径获取文件基本信息
  9. android——shortcut

随机推荐

  1. android kitkat(4.4以上)各个版本的特性解
  2. 使用Android(安卓)Studio开发FFmpeg的正
  3. Android(安卓)WebSocket通信通过Service
  4. 根据文字的多少,自动适应变化的表格...
  5. android camera 源码分析
  6. Android(安卓)SDK Manager 下载问题
  7. Android(安卓)文件及文件夹操作
  8. Android各个版本API的区别
  9. Android(安卓)动态设置布局属性
  10. 从xml添加menu注意事项