通常情况下我们会通过继承TabActivity,调用getTabHost()获取TabHost实例,下面是具体过程。
TabHostActivity.java
public class TabHostActivity extends TabActivity {
private TabHost tabHost;
private Intent certificateIntent;
private Intent feeIntent;
private Intent scoreIntent;
private Intent studyIntent;
private Intent moreIntent;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
initIntent();
addSpec();
}
/**
* 初始化各个tab标签对应的intent
*/
privatevoid initIntent() {
studyIntent = new Intent(this, StudyActivity.class);
scoreIntent = new Intent(this, ScoreActivity.class);
feeIntent = new Intent(this, FeeActivity.class);
certificateIntent = new Intent(this, CertificateActivity.class);
moreIntent = new Intent(this, MoreActivity.class);
}
/**
* 为tabHost添加各个标签项
*/
privatevoid addSpec() {
tabHost.addTab(this.buildTagSpec("tab_study",
R.string.study_progress,R.drawable.account01, studyIntent));
tabHost.addTab(this.buildTagSpec("tab_score",
R.string.test_score,R.drawable.account02, scoreIntent));
tabHost.addTab(this.buildTagSpec("tab_fee",
R.string.fee_pay,R.drawable.account03, feeIntent));
tabHost.addTab(this.buildTagSpec("tab_certificate", R.string.certificate_grant,R.drawable.accountcertificateIntent));
tabHost.addTab(this.buildTagSpec("tab_more", R.string.more,
R.drawable.account05, moreIntent));
}
/**
* 自定义创建标签项的方法
* @param tagName 标签标识
* @param tagLable 标签文字
* @param icon 标签图标
* @param content 标签对应的内容
* @return
*/
private TabHost.TabSpec buildTagSpec(String tagName, int tagLable,
int icon, Intent content) {
returntabHost
.newTabSpec(tagName)
.setIndicator(getResources().getString(tagLable),
getResources().getDrawable(icon)).setContent(content);
}}
运行结果如下图所示
我们发现标签位置处于界面上方,但是我们看到的很多应用的标签都处于界面底部。
如下图所示
我们要实现这种效果只需要将TabActivity的默认布局覆盖即可。新布局只需将标签和标签对应内容的相对位置调换一下就可以了,这里是用相对布局将标签对应内容的位置放到了标签的上方。不要改动id(会抛异常,提示必须要用指定的id)。不要忘了在onCreate()里设置新布局将TabActivity的默认布局覆盖。
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
tabHost = getTabHost();
initIntent();
addSpec();
}
tab.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- TabHost组件id值不可变-->
<TabHostxmlns:android=http://schemas.android.com/apk/res/android
android:id="@android:id/tabhost"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- TabWidget组件id值不可变-->
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
<!-- FrameLayout布局,id值不可变-->
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs">
</FrameLayout>
</RelativeLayout>
</TabHost>
通常在项目中我们都会有一个自定义的Activity基类,我们会让所有的界面Activity去继承这个基类。但是要使用TabHost就要继承TabActivity,所以我们可以定义两个基类,一个是普通Activity界面的基类,另一个是包含TabHost界面的基类,让这个基类继承TabActivity即可。
转自:http://www.cnblogs.com/wader2011/archive/2011/10/11/2207670.html
相关推荐
详细介绍了安卓中tabhost的用法,有助于初学者学习
Android TabHost用法详解,非常好的实例
android tabHost的基本用法
一、TabHost用法 通常情况下我们会通过继承TabActivity,调用getTabHost()获取TabHost实例,下面是具体过程。 TabHostActivity.java public class TabHostActivity extends TabActivity { private TabHost tabHost...
此文档对TabHost进行了系统的介绍,并且有使用步骤,后面更有例子。
自己写的程序 回家继续学习
TabHost+FrameLayout详细用法实例源代码
NULL 博文链接:https://geyubin.iteye.com/blog/1213902
学习tab,tabhost的朋友们,这是一个挺漂亮的tab,tabhost的源码实例,可以下载下来装在自己的eclipse上学习哦。
android TabHost全用法,欢迎下载学习交流
本文实例讲述了Android控件之TabHost用法。分享给大家供大家参考。具体如下: 以下通过TabHost实现android选项卡。 main.xml布局文件: <?xml version=1.0 encoding=utf-8?> android:layout_width=fill_...
TabHost的使用方法,适合初学者,适合课堂讲授实现模拟显示未接来电、已接来电和拨出电话的选项卡。
主要为大家详细介绍了Android Tabhost使用方法,如何利用TabHost 实现tab视图,感兴趣的小伙伴们可以参考一下
tab一个小例子,适合新手。 主要是在页面内写布局,和一个用不同xml文件书写每一个tab的布局。
Android中的选项卡是用TabHost实现的,下面我们用一个例子说明他的使用方法
这份Pdf文档主要讲解了TabHost控件的使用方法,希望对菜鸟们有帮助
TabHost介绍包含把默认的放在左面,很实用免积分,快来下载吧, 学习TabHost的用法 。