TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域。在手机系统的应用类似“未接电话”、“已接电话”、“呼出电话”等。

1 、 TabHost提供了两个方法来创建选项卡、加入选项卡

newTabSpec(String tag)  : 创建选项卡

addTab(TabHost.TabSpec  tabSpec) : 加入选项卡

2、TabHost 切换选项卡触发的监听是TabHost.OnTabChangeListener

以下通过案例来熟悉TabHost

使用TabHost的一般步骤为:

(1)在界面布局中为TabHost定义改选项卡的内容

(2)Activity继承TabActivity

(3)调用TabActivity的getTabHost()方法获取TabHost对象

(4)通过TabHost对象的方法来创建选项卡、加入选项卡

跟着上面步骤来实现TabHost案例

(1)在界面布局中为TabHost定义改选项卡的内容,一般採用FrameLayout作为根布局,每一个标签页面相应一个子节点的Layout

<!-- 这是根布局 --><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><!-- 这是第一个Tab布局 --><LinearLayout android:id="@+id/widget_layout_Blue"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical" ><EditText android:id="@+id/widget34"android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="EditText"android:textSize="18sp"></EditText><Button android:id="@+id/widget30"android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Button"></Button></LinearLayout><!-- 这是第二个Tab布局 --><LinearLayout android:id="@+id/widget_layout_red"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"  ><AnalogClock android:id="@+id/widget36"android:layout_width="wrap_content"android:layout_height="wrap_content"></AnalogClock></LinearLayout><!-- 这是第三个Tab布局 --><LinearLayout android:id="@+id/widget_layout_green"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"><RadioGroupandroid:id="@+id/widget43"android:layout_width="166px"android:layout_height="98px"android:orientation="vertical"><RadioButton android:id="@+id/widget44"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="男"></RadioButton><RadioButton android:id="@+id/widget45"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"></RadioButton></RadioGroup></LinearLayout></FrameLayout>

(2)Activity继承TabActivity

(3)调用TabActivity的getTabHost()方法获取TabHost对象

(4)通过TabHost对象的方法来创建选项卡、加入选项卡

package com.example.tabhost;import android.app.TabActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity
{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);//获取TabHost 对象TabHost tabHost = getTabHost();//设置使用TabHost布局//from(this)从这个TabActivity获取LayoutInflater//R.layout.main 存放Tab布局//通过TabHost获得存放Tab标签页内容的FrameLayout//是否将inflate 拴系到根布局元素上LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView(), true);//加入第一个标签页//setIndicator  加入表体,能够是view//加入tab内容   布局tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB1").setContent(R.id.widget_layout_Blue)); //加入第二个标签页tabHost.addTab(tabHost.newTabSpec("tab2")//在标签标题上放置图标.setIndicator("TAB2").setContent(R.id.widget_layout_red)); //加入第三个标签页tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("TAB3").setContent(R.id.widget_layout_green));  //加入监听tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {Log.i("Tab", tabId);}});}
}

执行后,效果例如以下:

android学习--TabHost选项卡组件相关推荐

  1. Android学习——UI高级组件三

    Android学习--UI高级组件三 PopupWindow(弹出式窗口) Android的对话框有两种:PopupWindow和AlertDialog.它们的不同点在于:AlertDialog位置固 ...

  2. Android学习之四大组件简单介绍

    组件是可以调用的基本功能模块.Android的应用程序就是由组件组成的,Android系统中有四个重要的组件,分别是Activity(活动).Service(服务).BroadcaseReceiver ...

  3. android学习之五·使用系统组件TabHost(使用布局文件)

     文/bywinkey 整理时间:2014年12月28日19:17:04 概述 在一个应用中,如果有多个标签页需要频繁切换时:例如 腾讯的微信和QQ,都是有用到多个功能页的切换,Android系统 ...

  4. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  5. Android学习笔记:TabHost 和 FragmentTabHost

    2019独角兽企业重金招聘Python工程师标准>>> Android学习笔记:TabHost 和 FragmentTabHostTabHost命名空间:android.widget ...

  6. Android学习笔记:TabHost 和 FragmentTabHost(转)

    Android学习笔记:TabHost 和 FragmentTabHost(转) 转自:http://www.cnblogs.com/asion/p/3339313.html 作者:Asion Tan ...

  7. android 界面组件,安卓开发学习周第三篇——Android中的UI组件

    原标题:安卓开发学习周第三篇--Android中的UI组件 在Android APP中,所有的用户界面元素都是由View和ViewGroup的对象构成的.View是绘制在屏幕上的用户能与之交互的一个对 ...

  8. android tabhost的使用方法,android TabHost(选项卡)的使用方法

    首先,定义TabHost的布局文件: android:id="@android:id/tabhost" android:layout_width="fill_parent ...

  9. 【Monica的android学习之路】四大组件的生命周期

    [Monica的android学习之路]四大组件的生命周期 1. Service 1.1 启动service 1.1.1 startService 1.1.2 bindService 1.2 死亡回调 ...

最新文章

  1. 火爆 GitHub!这个 AI 神器究竟有什么魅力?
  2. 对话腾讯AI Lab:即将开源自动化模型压缩框架PocketFlow,加速效果可达50%
  3. 人工智能将会如何影响和服务医疗行业?未来十年会有哪些值得期待的应用?
  4. 【组合数学】生成函数 ( 正整数拆分 | 无序 | 有序 | 允许重复 | 不允许重复 | 无序不重复拆分 | 无序重复拆分 )
  5. JDK 5.0 的新语法
  6. nsstring 空值比较_用比较器的nulls排序具有空值的列表
  7. 快速理解binary cross entropy 二元交叉熵
  8. php yii多表查询
  9. JAVA锁之公平锁和非公平锁
  10. c++11新特性的使用---可变模板参数、type_traits、function综合使用
  11. ubuntu使用CMake时报错compilation terminated找不到头文件解决方法
  12. jquery easyui 封装
  13. sharepoint2013爬xls文件:Error initializing IFilter for extension的解决方案
  14. 201671010129 2016—2017—2 《Java程序设计》Java总结
  15. 360全景地图 android,Android-谷歌VR展示360度全景图
  16. [转载] 过 DNF TP 驱动保护
  17. 访问网络共享找不到网络名的解决方案
  18. 手撕神经网络(1)——神经网络的基本组件
  19. glTF格式介绍——目录
  20. ConstraintLayout实现左中右布局

热门文章

  1. 新浪微博放开140字限制:社交向左 原创向右
  2. 深入理解Java Proxy机制
  3. 使用模板部署的Linux虚拟机网卡不可用的处理方法
  4. [泛读]4篇Web Service Replication方面论文
  5. 抽象数据类型(顺序栈)、断言、包含头文件、内联函数、非内联成员函数[C++ In Action][4]...
  6. 基于OpenVINO的多输入model optimizer(Tensorflow)
  7. C++学习之基本概念
  8. sql server 索引阐述系列五 索引参数与碎片
  9. ASP.NET CORE的Code Fist后Models更改了怎么办?
  10. 使用php-amqplib连接rabbitMQ 学习笔记及总结