Android 中的TabHost相当于VC或者Swing中的选项卡,本文中所提到的选项卡就代表TabHost。在Android中选项卡由TabActivity来实现,TabActivity是一个ActivityGroup,它持有TabHost对象,TabHost是一个View对象,它的基本构成如下面的布局文件所示

<?xml version="1.0" encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

android:id="@android:id/tabhost" android:layout_width="fill_parent"

android:layout_height="fill_parent">

"vertical"

android:layout_width="fill_parent" android:layout_height="fill_parent">

"@android:id/tabcontent"

android:layout_width="fill_parent" android:layout_height="wrap_content"

android:layout_weight="1" />

"@android:id/tabs" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:layout_weight="0" android:gravity="center" />

   FrameLayout表示选项卡内容显示的布局,TabWidget标识选项卡标签显示的布局 实际上一个TabHost包含一个FrameLayout和一个TabWidget,这个文件是我参照系统自带的tab_content文件修改的,只是将TabWidget的位置和FrameLayout的位置调换了一下,目的是为了使选项卡的标签显示在屏幕的下方,如果你没有自定义TabHost的布局,默认情况下选项卡标签是显示在屏幕上方的。 对于选项卡的内容,可以有三种形式:

<1>最常用的来自其他Activity

   TabHost.TabSpec indexTab=myTabHost.newTabSpec("index");//创建选项卡标签对象

   Intent  indexIntent=new Intent(current.this,Target.class);//选择此标签时,将要跳转 的Activity,这个Activity就显示在FrameLayout布局中。

   indexTab.setIndicator("标签名");//设置标签名也可以设置这个标签对应的图片

   indexTab.setContent(indexIntent);//设置标签的内容,

   myTabHost.addTab(indexTab);//将标签添加到选项卡

<2>来自布局文件中的View,这个布局文件必须是TabHost的内容布局

   LayoutInflater.from(this).inflate(R.layout.tab  myTabHost.getTabContentView(),true);

   TabHost.TabSpec indexTab=myTabHost.newTabSpec("index");

    indexTab.setIndicator("标签名");

   indexTab.setContent(R.id.yourview);

   //yourview 必须在tab.xml中已经定义了,tab.xml必须是FrameLayout

   myTabHost.addTab(indexTab);

    myTabHost.addTab(indexTab);

<3>来自通过Java代码实现的View

     TabHost.TabSpec indexTab=myTabHost.newTabSpec("index");

    indexTab.setIndicator("标签名");

   indexTab.setContent(new TabHost.TabContentFactory()

                            {

                             public View createTabContent(String tag) {

                             TextView myText=new TextView(currentActivity.this);

                             myText.setText(" 标签名");

                             Return myText;

                             }

                            }

);

 myTabHost.addTab(indexTab);

     Android系统中自带的tab_content.xml实际上和TabHost对象是对等的。通过分析

源代码就可以得出,TabActivity通过getTabHost()方法,就将ContentView设置为tab_content

如果不通过getTabHost()方法来初始化TabHost对象,为了简单起见,自己构造的xml文件也必须和tab_content.xml中定义的类似,就像我自己改的那样。View 的id必须为系统自带的id,如android:id/tabhost,因为TabActivity中很多方法的判断都是根据系统自带的id得出的。如果需要构造完全自定义的TabHost,就必须重写很多TabActivity的方法。如果只是将TabActivity用作一般UI显示,完全没有这个必要。

    具体实现如下:

第一步:让自己的Activity作为TabActivity的子类,持有一个TabHost对象myTabHost

第二步:实现自己的TabHost,编写对应的布局文件。如果只是通过getTabHost()来初始化TabHost(),可以写 myTabHost=getTabHost(),如果要通过自定义布局文件来实现,

myTabHost=(TabHost)findViewById(android.R.id.tabhost);

第三步:如果需要改变TabHost内容区域的布局

LayoutInflater.from(this).inflate(R.layout.mainmyTabHost.getTabContentView());

如果选项卡内容需用到main.xml中的View对象,就必须加上这一句,如果没有用到,就可以不写。

第四步:添加选项卡内容

TabHost.TabSpec indexTab=myTabHost.newTabSpec("index");

Intent indexIntent=new Intent(MicroBlogMain.this,MicroBlogIndex.class);

indexTab.setIndicator(getString(R.string.index));//也可以设置图标

indexTab.setContent(indexIntent);

myTabHost.addTab(indexTab);

myTabHost.setup();

更多相关文章

  1. Android(安卓)Studio的Gradle文件方法说明
  2. android动态加载jar/dex
  3. Android的文件系统
  4. Android(安卓)之ndk开发
  5. Android4.4支持使用DevTools对原生Android应用程序WebViews内容
  6. Android之——多线程下载示例
  7. Android(安卓)文件布局一些细节备忘
  8. 让Android中的webview支持页面中的文件上传
  9. zbar android sdk源码编译

随机推荐

  1. 如何开始为Android开发应用程序? [重复]
  2. android 怎么开启WPA2加密的热点
  3. (原+转)Eclipse中Android调用OpenCv
  4. Android的传感器HAL层的书写---基础篇
  5. 【金阳光测试】Android自动化测试第一季
  6. 自定义BaseAdapter,在主Activity页面调用
  7. 领域:更新外部对象的id
  8. 如何使用adb命令查看android中的数据库
  9. 如何优化Android中的位图绘制?
  10. Android开发中使用CRC校验