从零开始学android<TabHost标签组件.二十九.>

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

java.lang.Object

? android.view.View

? android.view.ViewGroup

? android.widget.FrameLayout

? android.widget.TabHost

常用方法如下所示


1

public TabHost(Context context)

构造

创建TabHost类对象

2

public void addTab(TabHost.TabSpec tabSpec)

普通

增加一个Tab

3

public TabHost.TabSpec newTabSpec(String tag)

普通

创建一个TabHost.TabSpec对象

4

public View getCurrentView()

普通

取得当前的View对象

5

public void setup()

普通

建立TabHost对象

6

public void setCurrentTab(int index)

普通

设置当前显示的Tab编号

7

public void setCurrentTabByTag(String tag)

普通

设置当前显示的Tab名称

8

public FrameLayout getTabContentView()

普通

返回标签容器

9

public void setOnTabChangedListener

(TabHost.OnTabChangeListener l)


普通

设置标签改变时触发

两种方式实现TabHost

方式一:直接让一个Activity程序继承TabActivity类;

方式二:利用findViewById()方法取得TagHost组件,并进行若干配置;

第一种方式让一个类继承tabActivity

XMl文件 配置需要在一个xml文件中嵌套使用布局 来达到不同Tab中显示不同的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_marginTop="50dp"
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="25sp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="25sp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="取消" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="登录" />
        </TableRow>
    </LinearLayout>

    <LinearLayout
        android:layout_marginTop="50dp"
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/a2" />
    </LinearLayout>

    <LinearLayout
        android:layout_marginTop="50dp"
        android:id="@+id/timer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
 <LinearLayout
        android:layout_marginTop="50dp"
        android:id="@+id/timer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
     <LinearLayout
        android:layout_marginTop="50dp"
        android:id="@+id/timer2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

JAVA文件设置

package com.example.tabhost;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
	private TabHost tabHost;//初始化TabHost组件
	private int reslayout[] = { R.id.login, R.id.image, R.id.timer , R.id.timer1,R.id.timer2};//设置对应的额xml文件
	private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//设置显示的标题文件

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.tabHost = super.getTabHost();//实例化TabHost组件
		// 取得LayoutInflater对象
		LayoutInflater.from(this).inflate(R.layout.linearlayout,//制定布局
				this.tabHost.getTabContentView(),//制定标签增加的容器
				true);

		for (int i = 0; i < reslayout.length; i++) {
			TabSpec myTab = tabHost.newTabSpec("tab" + i);// 定义TabSpec
			myTab.setIndicator(null,getResources().getDrawable(images[i])) ;		// 设置标签

			myTab.setContent(this.reslayout[i]) ;		// 设置显示的组件
			this.tabHost.addTab(myTab) ;
		}
	}

}

效果图如下


使用配置文件设置。

这种方法较为常见,可以讲TabHost置于底部

但是布局文件较为复杂,大家可以参照例子进行具体的学习

XML文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/tabhost"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<RelativeLayout
		android:orientation="vertical"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
	<TabWidget
		android:id="@android:id/tabs"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:layout_alignParentBottom="true" />
		<FrameLayout
			android:id="@android:id/tabcontent"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent">
			 <LinearLayout

        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="25sp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="25sp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="取消" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="登录" />
        </TableRow>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/a2" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/timer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
 <LinearLayout
        android:id="@+id/timer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
     <LinearLayout
        android:id="@+id/timer2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
		</FrameLayout>
	</RelativeLayout >
</TabHost>

JAVA文件配置

package com.example.tabhost;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {			// 直接继承Activity
	private TabHost myTabHost;							// 定义TabHost
	private int[] layRes = { R.id.login, R.id.image
			, R.id.timer, R.id.timer1, R.id.timer2 };							// 定义内嵌布局管理器ID
	private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//标题图片数据

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_main) ;			// 调用默认布局管理器
		this.myTabHost = (TabHost) super.findViewById(R.id.tabhost); // 取得TabHost对象
		this.myTabHost.setup() ;						// 建立TabHost对象
		for (int x = 0; x < this.layRes.length; x++) {	// 循环取出所有布局标记
			TabSpec myTab = myTabHost.newTabSpec("tab" + x);	// 定义TabSpec
			myTab.setIndicator(null,getResources().getDrawable(images[x])) ;			// 设置标签文字
			myTab.setContent(this.layRes[x]) ;			// 设置显示的组件
			this.myTabHost.addTab(myTab) ;				// 增加标签
		}
		this.myTabHost.setCurrentTab(0) ;				// 设置开始索引
	}
}

使用TabHost组件设置Tab切换与intent的结合在开发中较常用到,是app开发框架的基础

下节预报:

Menu菜单

时间: 2024-08-28 01:17:45

从零开始学android<TabHost标签组件.二十九.>的相关文章

从零开始学android&lt;android事件的处理方式.二十四.&gt;

在android中一共有 多种事件,每种事件都有自己相对应的处理机制 如以下几种 1 单击事件 View.OnClickListener public abstract void onClick (View v) 单击组件时触发 2 单击事件 View.OnLongClickListener public abstract boolean onLongClick (View v) 长按组件时触发 3 键盘事件 View.OnKeyListener public abstract boolean

二十四、Android文件的读写

Android的文件读写与JavaSE的文件读写相同,都是使用IO流.而且Android使用的正是JavaSE的IO流,下面我们通过一个练习来学习Android的文件读写. 1.创建一个Android工程 [html] view plaincopy Project name:File BuildTarget:Android2.2 Application name:文件读写 Package name:test.file Create Activity:DateActivity Min SDK Ve

Android学习笔记二十四之ListView列表视图二

Android学习笔记二十四之ListView列表视图二 前面一篇我们介绍了常用的几种适配器的简单实现和ListView的简单使用,这一篇中,我们介绍一下ListView的优化和一些其它的问题. ListView优化方法一 在ListView中,我们最常用的就是自定义Adapter,在我们自定义Adapter中,需要实现两个比较重要的方法getCount()和getView(),前者是负责计算ListView的总Item数,后者是生成Item,有多少个Item就会调用getView()方法多少次

Android学习路线(二十四)ActionBar Fragment运用最佳实践

通过前面的几篇博客,大家看到了Google是如何解释action bar和fragment以及推荐的用法.俗话说没有demo的博客不是好博客,下面我会介绍一下action bar和fragment在实战中的应用,以及相关demo源码,希望和大家相互交流. 了解过fragment的同学们应该都知道,fragment是android 3.0版本才出现的的,因此如果要在支持android 3.0一下版本的工程中使用fragment的话是需要添加Support Library的.具体如何添加我就不再赘述

Android 沉浸式状态栏 实现方式二 ( 更简单 )

以前写过一个沉浸式状态栏 的实现方式 Android 沉浸式状态栏 实现方式一 现在有个更为简单的实现方式 . 相关链接 http://www.apkbus.com/forum.php?mod=viewthread&tid=255929&extra=page%3D3%26filter%3Dsortid%26orderby%3Ddateline%26sortid%3D12 1.效果图 demo 的 github 地址  https://github.com/zyj1609wz/Android

Android笔记二十四.Android基于回调的事件处理机制

如果说事件监听机制是一种委托式的事件处理,那么回调机制则与之相反,对于基于回调的事件处理模型来说,事件源和事件监听器是统一的,或者说事件监听器完全消失了,当用户在GUI控件上激发某个事件时,控件自己特定的方法将会负责处理该事件. 转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空) 一.View类的常见回调方法 为了使用回调机制来处理GUI控件上所发生的事件,需要为该组件提供对应的事件处理方法,而Java又是一种静态语言,我们无法为每个对象动态地添

Android开发系列(二十四):Notification的功能与用法

关于消息的提示有两种:一种是Toast,一种就是Notification.前者维持的时间比较短暂,后者维持的时间比较长. 而且我们平常手机的应用比如网易.贴吧等等都有很多的推送消息,就是用Notification实现的. Notification是显示在手机状态栏的通知-手机状态栏位于手机屏幕的上方.程序一般通过NotificationManager服务来发送Notification通知 Notification的一些方法,接下来我们都能够用到: setDefaults():设置通知LED等.音

Android学习笔记二十四.Service入门(二)绑定本地Service并与之通信

绑定本地Service并与之通信    通过上一篇博文的前3步,我们就算完成了一个Service及使用该Service的应用程序(Service为该应用程序的组成部分).但当程序通过startService()和stopService()启动.关闭Service时,Service与访问者之间基本上不存在太多的关联,因此Service和访问者之间也无法进行通信.数据交换.如果我们希望开发的Service能与访问者之间实现方法调用或数据交换,我们可以让访问者使用bindService()和unbin

Android开发系列(二十四):Notification的功能与使用方法

关于消息的提示有两种:一种是Toast,一种就是Notification.前者维持的时间比較短暂,后者维持的时间比較长. 并且我们寻常手机的应用比方网易.贴吧等等都有非常多的推送消息.就是用Notification实现的. Notification是显示在手机状态栏的通知-手机状态栏位于手机屏幕的上方.程序一般通过NotificationManager服务来发送Notification通知 Notification的一些方法.接下来我们都可以用到: setDefaults():设置通知LED等.

《Android源码设计模式解析与实战》读书笔记(二十四)

第二十四章.桥接模式 桥接模式也称为桥梁模式,是结构型设计模式之一.桥接模式中体现了"单一职责原则"."开闭原则"."里氏替换原则"."依赖倒置原则"等.同时它也是很实用的一种模式. 1.定义 将抽象部分与现实部分分离,使它们都可以独立地进行变化. 2.使用场景 (1)如果一个系统需要在构建的抽象化角色和具体角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系. (2)对于那些不希望使用继承或因为多层次继承导致系统类