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">
<RadioGroup
android: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
{
@Override
public 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() { @Override
public void onTabChanged(String tabId) {
Log.i("Tab", tabId); }
});
}
}

执行后,效果例如以下:

android学习--TabHost选项卡组件的更多相关文章

  1. 从零開始学android&lt;TabHost标签组件.二十九.&gt;

    TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...

  2. Android学习Tabhost、gallery、listview、imageswitcher

    Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...

  3. Android学习笔记⑧——UI组件的学习AdapterView相关2

    前面都是用ListView控件来配合Adapter做的一些实例,这次我们来见识一下GridView与Adapter之间的爱恨情仇.... GridView是用于在界面上按行.列分布的方式来显示多个的组 ...

  4. Android学习笔记⑦——UI组件的学习AdapterView相关1

    AdapterView是一个非常重要的组件之一,他非常灵活,所以得好好学...AdapterView本身是一个抽象类,派生出来的子类用法也十分相似,只是界面有一定的区别,因此本节把他们归为一类 Ada ...

  5. Android学习笔记⑥——UI组件的学习ImageView相关

    ImageView是集成了View的组件,它的主要工作就是显示一些图片啊,虽然他的用法一句话概括了,但是我觉得学起来应该不会太简单,正所谓 短小而精悍么 :) ImageView 派生了 ImageB ...

  6. Android学习笔记⑤——UI组件的学习TextView相关

    TextView是一个强大的视图组件,直接继承了View,同时也派生出了很多子类,TextView其作用说白了就是在布局中显示文本,有点像Swing编程中的JLabel标签,但是他比JLabel强大的 ...

  7. Android学习:自定义组件,DrawView

    布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  8. android学习之EdieText组件的使用

    界面如下 移通152余继彪 该界面由四个EditText组件和Button按钮还有一个通知Toast完成,首先在xml文件中添加了四个组件和一个按钮还有一个文字显示框,java代码部分为button添 ...

  9. Android学习总结——Service组件

    从Service的启动方式上,可以将Service分为Started Service和Bound Service.在使用Service时,要想系统能够找到此自定义Service,无论哪种类型,都需要在 ...

随机推荐

  1. maven pom.xml具体解释(整理)

    pom作为项目对象模型. 通过xml表示maven项目,使用pom.xml来实现.主要描写叙述了项目:包含配置文件.开发人员须要遵循的规则,缺陷管理系统.组织和licenses,项目的url,项目的依 ...

  2. C++中的对象数组

    类是对象的抽象,我们可以使用一个类来定义很多的对象,然后每个对象都有自己的属性. 当我们使用类来定义很多相同结构的对象的时候,我们可以采取对象数组的方法. 例如,一个班有50个学生,我们定义了一个学生 ...

  3. python进阶之路之文件处理

    Python之文件处理 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  4. .NET基础拾遗(3)字符串、集合和流2

    二.常用集合和泛型 2.1 int[]是值类型还是引用类型? .NET中无论是存储值类型对象的数组还是存储引用类型的数组,其本身都是引用类型,其内存也都是分配在堆上的.所有的数组类型都继承自Syste ...

  5. React数据传递

    React基础概念 React是基于组件化的开发,通过组件的组合,让web应用能够实现桌面应用的效果. React更有利于单页应用的开发. 并非MVC框架,只能算是V 具有单项数据流的特点 优势:代码 ...

  6. .NET程序猿 - 提升幸福感的组件一览

    1.Newtonsoft.Json.net 操作JSON最简便的方式.  .Net 3.5开始,Framework集成Json序列化器:JavaScriptSerializer,然而Json.net给 ...

  7. C#扩展方法的理解

    “扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些 ...

  8. oracle插入例子

    string sql = "insert into EMST_JC_SBXX(XL,SBBM,SBWH,SBMC,CCBM,XNCS,CZXL,ZL,GL,ZZCJ,TCRQ,SYQX,XH ...

  9. show_space/get_alert_log/get_trace_file

    1.get_alert_log 获取alert文件的路径和名称 set serveroutput on        --设置输出,让sqlplus在屏幕上可以输出.(要加入到login.sql中!) ...

  10. seajs模块压缩问题

    在优化整理项目代码时,想使用seajs来把代码模块化.看了下官方5分钟上手教程,觉得很不错,也没多想就一直开发下去了,也没出什么问题.等一同事说把代码打包个放到设备上去测试一下,发现怎么也跑不起来,郁 ...