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(安卓)Ant Build简单总结
  4. android如何打开系统wifi、蓝牙等设置界面
  5. Android内存管理策略的优化
  6. Android(安卓)绘制2D图形
  7. Android之怎么隐藏EditText光标和自动显示键盘
  8. Android开发设置Activity全屏与不全屏的方法
  9. APK 的自我保护

随机推荐

  1. Android 自定义动画 单个View平面位移以
  2. Android学习笔记:常用控件 RadioGroup和Ch
  3. 【eoe Android特刊】第二十五期 Android
  4. Android:(13)Intent消息传递
  5. android:layout_marginLeft指该控件距离
  6. Android图形显示系统——下层显示4:图层合
  7. MaterialDesgin之MaterialTextField
  8. 关于Android进程知识,你需要知道这些(一)
  9. Android中图片实现按钮点击效果
  10. android依赖工程 java build path androi