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

<?xmlversion="1.0"encoding="utf-8"?>

<TabHostxmlns:android="http://schemas.android.com/apk/res/android"

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

android:layout_height="fill_parent">

<LinearLayoutandroid:orientation="vertical"

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

<FrameLayoutandroid:id="@android:id/tabcontent"

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

android:layout_weight="1"/>

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

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

</LinearLayout>

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

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

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

IntentindexIntent=newIntent(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.TabSpecindexTab=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.TabSpecindexTab=myTabHost.newTabSpec("index");

indexTab.setIndicator("标签名");

indexTab.setContent(newTabHost.TabContentFactory()

{

publicViewcreateTabContent(Stringtag){

TextViewmyText=newTextView(currentActivity.this);

myText.setText("标签名");

ReturnmyText;

}

}

);

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.main,myTabHost.getTabContentView());

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

第四步:添加选项卡内容

TabHost.TabSpecindexTab=myTabHost.newTabSpec("index");

IntentindexIntent=newIntent(MicroBlogMain.this,MicroBlogIndex.class);

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

indexTab.setContent(indexIntent);

myTabHost.addTab(indexTab);

myTabHost.setup();

本文出自 “技术人生” 博客,转载请与作者联系!

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. NPM 和webpack 的基础使用
  3. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  4. Android中的签名机制
  5. Android(安卓)studio Failure [INSTALL_FAILED_OLDER_SDK]
  6. Android(安卓)studio 添加第三方库(转载)
  7. [Android] AutoCompleteTextView:自动完成输入内容的控件
  8. [原]如何在Android用FFmpeg解码图像
  9. Android与标准Linux对比

随机推荐

  1. Android(安卓)去掉系统默认提示音、移除
  2. Android(安卓)UI性能优化(一)
  3. android中的dp、px、dip相关概念
  4. kotlin 实现一个简单 Android(安卓)路由(2
  5. Android开发中使用sqlite实现新闻收藏和
  6. Android获取内置sdcard跟外置sdcard路径
  7. 通过分析Google的todo-mvp深度解析Androi
  8. Android中选项卡TabHost的实际应用篇
  9. Android绘图机制(四)——使用HelloCharts开
  10. Android(安卓)UI用户界面开发辅助工具(值