TabHost 简单用法
package com.google.tabhost;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class HelloTabHost extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//调用TabActivity的getTabHost()方法获取TabHost对象
TabHost tabHost = getTabHost();
//设置使用TabHost布局
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
//加入第一个标签页
tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01));
//加入第二个标签页,并在其标签上加入一个图片
tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));
//加入第三个标签页
tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03));
}
}
1、对于一个没有被加载或者想要动态加载的界面, 都须要使用inflate来加载.
2、对于一个已经加载的Activity, 就能够使用实现了这个Activiyt的的findViewById方法来获得当中的界面元素.
方法:
Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来加载一个在xml里定义好的界面.
事实上在Activity里面就使用了LayoutInflater来加载界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法能够获得一个 LayoutInflater, 也能够通过LayoutInflater
inflater = getLayoutInflater();来获得.然后使用inflate方法来加载layout的xml,
ImageView,右边TextView)。
TabSpec与TabHost
TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的每个分页面。d在Android中,每个TabSpec分布能够是一个组件,也能够是一个布局,然后将每个分页装入TabHost中,TabHost就可以将当中的每个分页一并显示出来
Android 中的Tabhost控件是个挺好用的控件,像一些分模块展示的页面就能够用Tabhost。

Tabhost的主要是由TabSpac组成的选项卡集合。TabSpec主要有两个重要方法,看代码:
- /**
- * A tab has a tab indicator, content, and a tag that is used to keep
- * track of it. This builder helps choose among these options.
- *
- * For the tab indicator, your choices are:
- * 1) set a label
- * 2) set a label and an icon
- *
- * For the tab content, your choices are:
- * 1) the id of a {@link View}
- * 2) a {@link TabContentFactory} that creates the {@link View} content.
- * 3) an {@link Intent} that launches an {@link android.app.Activity}.
- */
- public class TabSpec {
- private String mTag;
- private IndicatorStrategy mIndicatorStrategy;
- private ContentStrategy mContentStrategy;
- private TabSpec(String tag) {
- mTag = tag;
- }
- /**
- * Specify a label as the tab indicator.
- */
- public TabSpec setIndicator(CharSequence label) {
- mIndicatorStrategy = new LabelIndicatorStrategy(label);
- return this;
- }
- /**
- * Specify a label and icon as the tab indicator.
- */
- public TabSpec setIndicator(CharSequence label, Drawable icon) {
- mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);
- return this;
- }
- /**
- * Specify a view as the tab indicator.
- */
- public TabSpec setIndicator(View view) {
- mIndicatorStrategy = new ViewIndicatorStrategy(view);
- return this;
- }
- /**
- * Specify the id of the view that should be used as the content
- * of the tab.
- */
- public TabSpec setContent(int viewId) {
- mContentStrategy = new ViewIdContentStrategy(viewId);
- return this;
- }
- /**
- * Specify a {@link android.widget.TabHost.TabContentFactory} to use to
- * create the content of the tab.
- */
- public TabSpec setContent(TabContentFactory contentFactory) {
- mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
- return this;
- }
- /**
- * Specify an intent to use to launch an activity as the tab content.
- */
- public TabSpec setContent(Intent intent) {
- mContentStrategy = new IntentContentStrategy(mTag, intent);
- return this;
- }
/**
* A tab has a tab indicator, content, and a tag that is used to keep
* track of it. This builder helps choose among these options.
*
* For the tab indicator, your choices are:
* 1) set a label
* 2) set a label and an icon
*
* For the tab content, your choices are:
* 1) the id of a {@link View}
* 2) a {@link TabContentFactory} that creates the {@link View} content.
* 3) an {@link Intent} that launches an {@link android.app.Activity}.
*/
public class TabSpec { private String mTag; private IndicatorStrategy mIndicatorStrategy;
private ContentStrategy mContentStrategy; private TabSpec(String tag) {
mTag = tag;
} /**
* Specify a label as the tab indicator.
*/
public TabSpec setIndicator(CharSequence label) {
mIndicatorStrategy = new LabelIndicatorStrategy(label);
return this;
} /**
* Specify a label and icon as the tab indicator.
*/
public TabSpec setIndicator(CharSequence label, Drawable icon) {
mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);
return this;
} /**
* Specify a view as the tab indicator.
*/
public TabSpec setIndicator(View view) {
mIndicatorStrategy = new ViewIndicatorStrategy(view);
return this;
} /**
* Specify the id of the view that should be used as the content
* of the tab.
*/
public TabSpec setContent(int viewId) {
mContentStrategy = new ViewIdContentStrategy(viewId);
return this;
} /**
* Specify a {@link android.widget.TabHost.TabContentFactory} to use to
* create the content of the tab.
*/
public TabSpec setContent(TabContentFactory contentFactory) {
mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
return this;
} /**
* Specify an intent to use to launch an activity as the tab content.
*/
public TabSpec setContent(Intent intent) {
mContentStrategy = new IntentContentStrategy(mTag, intent);
return this;
}
setIndicator()能够设置选项卡得图标和文字。
须要注意几点是: 1、假设你的Tabhost是从xml文件里findViewById()得到的,
TabWidget 必须为 android:id="@android:id/tabs" ,
FrameLayout android:id="@android:id/tabcontent" ;
- <TabHost
android:id="@+id/tabhost_info"
android:layout_width="fill_parent" - android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"> - <LinearLayout
android:id="@+id/linearLayout" - android:layout_width="fill_parent"
android:layout_height="fill_parent" - android:orientation="vertical">
- <TabWidget
android:id="@android:id/tabs" - android:layout_width="fill_parent"
android:layout_height="wrap_content"> - </TabWidget>
- <FrameLayout
android:id="@android:id/tabcontent" - android:layout_width="fill_parent"
android:layout_height="wrap_content" - android:layout_gravity="fill">
- <include
android:id="@+id/info_include01"
layout="@layout/info_layout01"
/> - <include
android:id="@+id/info_include02"
layout="@layout/info_layout02"
/> - <include
android:id="@+id/info_include03"
layout="@layout/info_layout03"
/> - </FrameLayout>
- </LinearLayout>
- </TabHost>
<TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:id="@+id/linearLayout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="fill">
<include android:id="@+id/info_include01" layout="@layout/info_layout01" />
<include android:id="@+id/info_include02" layout="@layout/info_layout02" />
<include android:id="@+id/info_include03" layout="@layout/info_layout03" /> </FrameLayout>
</LinearLayout> </TabHost>
2、代码中,在加入TabWidget前,须要调用setup()方法。
- tabHost=(TabHost)findViewById(R.id.tabhost_info);
- tabHost.setup();
- tabHost.addTab(tabHost.newTabSpec("信息")
- .setContent(R.id.info_include01)
- .setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher))
- );
- tabHost.addTab(tabHost.newTabSpec("很多其它信息")
- .setContent(R.id.info_include02)
- .setIndicator("很多其它信息",getResources().getDrawable(R.drawable.ic_launcher))
- tabHost.addTab(tabHost.newTabSpec("附件下载")
- .setContent(R.id.info_include03)
- .setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher))
tabHost=(TabHost)findViewById(R.id.tabhost_info);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("信息")
.setContent(R.id.info_include01)
.setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher)) );
tabHost.addTab(tabHost.newTabSpec("很多其它信息")
.setContent(R.id.info_include02)
.setIndicator("很多其它信息",getResources().getDrawable(R.drawable.ic_launcher)) );
tabHost.addTab(tabHost.newTabSpec("附件下载")
.setContent(R.id.info_include03)
.setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher)) );
以下是自己写的一个demo:


Demo下载地址 :http://download.csdn.net/detail/china1988s/4072957
。
TabHost 简单用法的更多相关文章
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- TabHost的用法(转)
本文结合源代码和实例来说明TabHost的用法. 使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图: 查看tabhost的源代码,主要实例变量有: pr ...
- NSCharacterSet 简单用法
NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...
- [转]Valgrind简单用法
[转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...
- Oracle的substr函数简单用法
substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ...
- Ext.Net学习笔记19:Ext.Net FormPanel 简单用法
Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...
- TransactionScope简单用法
记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...
- WPF之Treeview控件简单用法
TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...
随机推荐
- VBS解析时候遇到时间
http://msdn.microsoft.com/en-us/library/aa393687(v=vs.85).aspx MSDN说的很详细么. http://msdn.microsoft.com ...
- web编码(转)
问题2.浏览器编码方式是根据“响应标头-response header”中的键为“Content-Type”的值来自动选择判断,而不会简单的根据你在html中看到的标签值<meta http-e ...
- Hadoop-Yarn-HA集群搭建(搭建篇)
1.前提条件 我学习过程是一块一块深入的,在把hdfs基本弄懂以及HA成功的情况开始尝试搭建yarn的,建议在搭建前先去看一下转载的原理篇,懂了原理后搭建会很快的,再次强调一下hdfs我默认已经搭建成 ...
- iOS 性能测试 - FBMemoryProfiler
FBMemoryProfiler 是Facebook开源的一款用于分析iOS内存使用和检测循环引用的工具库. 脑补:http://www.cocoachina.com/ios/20160421/159 ...
- Hadoop学习之编译eclipse插件
近期准备開始学习Hadoop1.2.1的源码,感觉最好的方法还是能够在运行Hadoop及hadoop作业时跟踪调试代码的实际运行情况.因为选择的IDE为eclipse,所以准备编译一下hadoop的e ...
- PHP和JAVASCRIPT判断访客终端是电脑还是手机
当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点.本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型. PHP ...
- ASP.NET仿新浪微博下拉加载更多数据瀑布流效果
闲来无事,琢磨着写点东西.貌似页面下拉加载数据,瀑布流的效果很火,各个网站都能见到各式各样的展示效果,原理大同小异.于是乎,决定自己写一写这个效果,希望能给比我还菜的菜鸟们一点参考价值. 在开始之前, ...
- 搭建C#框架 博文观感
最近刚开始着手做项目,在后期开发的时候遇到不少预期之外的问题,而且工期也超出预算不少.反思了一下,主要是做的项目少,前期需求分析不明朗,当然对于框架也没有意识.凡此种种.当然,遇到问题就要去想办法解决 ...
- iOS状态栏字体设置为白色
info.plist 添加字段: view controller -base status bar appearence 设为NO [[UIApplication sharedApplication] ...
- 转载:C# Office 开发
原文地址:http://blog.sina.com.cn/s/blog_604fb7ae0100x2s7.html 中小企业办公自动化系统都需要有与微软办公软件连接的功能,如把数据导入到电子表格.Wo ...