2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > Android Tab 选项卡的简单实现

Android Tab 选项卡的简单实现

时间:2020-11-25 02:35:02

相关推荐

Android Tab 选项卡的简单实现

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

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

<TabHostxmlns:android="/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();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。