TabHost简单使用案例

Android 中的Tabhost控件是个挺好用的控件,像一些分模块展示的页面就可以用Tabhost。

Tabhost的主要是由TabSpac组成的选项卡集合。TabSpec主要有两个重要方法,看代码:

[java] view plaincopy

  1. /**
  2. * A tab has a tab indicator, content, and a tag that is used to keep
  3. * track of it.  This builder helps choose among these options.
  4. *
  5. * For the tab indicator, your choices are:
  6. * 1) set a label
  7. * 2) set a label and an icon
  8. *
  9. * For the tab content, your choices are:
  10. * 1) the id of a {@link View}
  11. * 2) a {@link TabContentFactory} that creates the {@link View} content.
  12. * 3) an {@link Intent} that launches an {@link android.app.Activity}.
  13. */
  14. public class TabSpec {
  15. private String mTag;
  16. private IndicatorStrategy mIndicatorStrategy;
  17. private ContentStrategy mContentStrategy;
  18. private TabSpec(String tag) {
  19. mTag = tag;
  20. }
  21. /**
  22. * Specify a label as the tab indicator.
  23. */
  24. public TabSpec setIndicator(CharSequence label) {
  25. mIndicatorStrategy = new LabelIndicatorStrategy(label);
  26. return this;
  27. }
  28. /**
  29. * Specify a label and icon as the tab indicator.
  30. */
  31. public TabSpec setIndicator(CharSequence label, Drawable icon) {
  32. mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);
  33. return this;
  34. }
  35. /**
  36. * Specify a view as the tab indicator.
  37. */
  38. public TabSpec setIndicator(View view) {
  39. mIndicatorStrategy = new ViewIndicatorStrategy(view);
  40. return this;
  41. }
  42. /**
  43. * Specify the id of the view that should be used as the content
  44. * of the tab.
  45. */
  46. public TabSpec setContent(int viewId) {
  47. mContentStrategy = new ViewIdContentStrategy(viewId);
  48. return this;
  49. }
  50. /**
  51. * Specify a {@link android.widget.TabHost.TabContentFactory} to use to
  52. * create the content of the tab.
  53. */
  54. public TabSpec setContent(TabContentFactory contentFactory) {
  55. mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
  56. return this;
  57. }
  58. /**
  59. * Specify an intent to use to launch an activity as the tab content.
  60. */
  61. public TabSpec setContent(Intent intent) {
  62. mContentStrategy = new IntentContentStrategy(mTag, intent);
  63. return this;
  64. }

setIndicator()可以设置选项卡得图标和文字。

需要注意几点是: 1、如果你的Tabhost是从xml文件中findViewById()得到的,

                            TabWidget 必须为 android:id="@android:id/tabs" ,

                             FrameLayout android:id="@android:id/tabcontent" ;

[html] view plaincopy

  1. <TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent"
  2. android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
  3. <LinearLayout android:id="@+id/linearLayout"
  4. android:layout_width="fill_parent" android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TabWidget android:id="@android:id/tabs"
  7. android:layout_width="fill_parent" android:layout_height="wrap_content">
  8. </TabWidget>
  9. <FrameLayout android:id="@android:id/tabcontent"
  10. android:layout_width="fill_parent" android:layout_height="wrap_content"
  11. android:layout_gravity="fill">
  12. <include android:id="@+id/info_include01" layout="@layout/info_layout01" />
  13. <include android:id="@+id/info_include02" layout="@layout/info_layout02" />
  14. <include android:id="@+id/info_include03" layout="@layout/info_layout03" />
  15. </FrameLayout>
  16. </LinearLayout>
  17. </TabHost>

 2、代码中,在添加TabWidget前,需要调用setup()方法。

[java] view plaincopy

  1. tabHost=(TabHost)findViewById(R.id.tabhost_info);
  2. tabHost.setup();
  3. tabHost.addTab(tabHost.newTabSpec("信息")
  4. .setContent(R.id.info_include01)
  5. .setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher))
  6. );
  7. tabHost.addTab(tabHost.newTabSpec("更多信息")
  8. .setContent(R.id.info_include02)
  9. .setIndicator("更多信息",getResources().getDrawable(R.drawable.ic_launcher))
  10. tabHost.addTab(tabHost.newTabSpec("附件下载")
  11. .setContent(R.id.info_include03)
  12. .setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher))

下面是自己写的一个demo:

          

时间: 2024-08-10 23:05:56

TabHost简单使用案例的相关文章

【初学菜鸟作--邮件服务的简单配置案例】

邮件服务器的配置以及使用 实验一:                    实验目的:简单搭建出邮件服务器并测试其可用性                    实验环境:DNS服务器一台,安装有Portfix的邮件服务器一台 实验步骤: 一.邮件的发送(SMTP) 1.在邮件服务器配置主机名,ip,并安装portfix并启动 [[email protected]~]# tail -2 /etc/sysconfig/network HOSTNAME=mail.tarena.com   [[email

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. */

Sharepoint构建轻量型应用之InfoPath的简单应用案例!

1.   很遗憾,在office 2016 版本中已经将表单功能移除,office 2013 版本已经成了infopath 绝唱,所以我们必须安装office 2013 版本的office Professional plus来完成我们的表单组件安装: 2.   点开infopath Deisgner,我们可以看到可以创建的组件: 3.   这里我们可以创建多种表单,我们这次做一个简单的案例,建立一个标准的空白表单后点击设计后进入到相应的表单设计界面,我们先设计好整个页面的布局: 4.   基于简

_00019 Storm的体系结构介绍以及Storm入门案例(官网上的简单Java案例)

博文作者:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前,妳却感觉不到我的存在 技术方向:Flume+Kafka+Storm+Redis/Hbase+Hadoop+Hive+Mahout+Spark ... 云计算技术 转载声明:可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明,谢谢合作! qq交流群:214293307  (期待与你一起学习,共同进步) # Storm

jquery,tree无限级树形菜单+简单实用案例

jquery,tree无限级树形菜单+简单实用案例 我在项目中用到产品类别的树形.各种地方都要用. 我就封装起来,方便以后调用. 记录下来,希望给新手们提供帮助.要记得导入jquery.js  tree.js 哦 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri=&quo

springcloud+eureka简单入门案例

springcloud+eureka简单入门案例 一.服务提供者 直接提供服务,入门案例没有特别要设置的地方,注意下端口,由于要启动多个服务,可能会冲突 配置文件(src/main/resources/application.yml) server: port: 8000 二.服务消费者 服务消费者的依赖在这个单独的demo中其实可有可无,亲测不添加,也可以实现demo服务提供能 三.服务消费者启动类里注入RestTemplate,用于调用远程服务 import org.springframew

join连接的五种方式的简单使用案例(Inner join,Left join,Right join

1.内连接Inner join 内连接是基于连接谓词将俩张表(如A和B)的列组合到一起产生新的结果表 ,在表中存在至少一个匹配时,INNER JOIN 关键字返回行.  下面是一个简单的使用案例  以下是运行代码及结果  2.左外连接Left join 左外连接Left join关键字会从左表那里返回所有的行,即使是在右表中没有匹配到的行  下面是一个简单的案例  下面是测试用例  3.右外连接Right join 右外连接关键字Right join会从右表那里返回所有的行,即使是在左表中没有匹

Python 简单爬虫案例

Python 简单爬虫案例 import requests url = "https://www.sogou.com/web" # 封装参数 wd = input('enter a word') param = { 'query':wd } response = requests.get(url=url,params=param) page_text = response.content fileName = wd+'.html' with open(fileName,'wb') as

layout方法的简单使用案例

layout代表着某一个view在父view中的位置,比如 ViewGroup parent = new ViewGroup(); View child = new View(); parent.addView(child) child.layout(l,t,r,b); ps:用new 来初始化对象只是为了方便说明情况 此时child是parent的子View,child.layout(l,t,r,b)就说明了child在parent放置的坐标位置,坐标原点(0,0)就是parent的左上角.