过时但仍值得学习的选项卡TabHost

时间:2022-04-26
本文章向大家介绍过时但仍值得学习的选项卡TabHost,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

今天来了解一个过时的组件,了解的目的不是学会用起来开发,而是了解这种界面的设计和其特点,后期可以用其他方式来替代。

一、TabHost概述

TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆放区域。通过这种方式,就可以在一个容器里放置更多组件。

TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的标签,FrameLayout是Tab内容。与TabHost结合使用的有如下2个组件。

  • TabWidget:代表选项卡的标题条。
  • TabSpec:代表选项卡的一个Tab页面。

TabHost仅仅是一个简单的容器,它提供了如下两个方法来创建、添加标签页。

  • newTabSpec(String tag):创建选项卡。
  • addTab(TabHost.TabSpec tabSpec):添加选项卡。

实现TabHost有两种方式:

  • 直接让一个Activity程序继承TabActivity类。
  • 定义XML布局文件利用findViewById()方法取得TagHost组件,通过setup()方法实例化并进行配置。

二、继承TabActivity实现

通过继承TabActivity类,使用TabHost的一般步骤如下。

  1. 在界面布局文件中定义TabHost组件,并为该组件定义该选项卡的内容。
  2. Activity 应该继承 TabActivity。
  3. 调用 TabActivity 的 getTabHost()方法获取 TabHost 对象。
  4. 通过TabHost对象的方法来创建、添加选项卡。

除此之外,TabHost还提供了一些方法来获取当前选项卡。如果程序需要监控TabHost里当前标签页的改变,则可以为它设置 TabHost.OnTabChangeListener 监听器。

接下来通过一个简单的示例程序来学习TabHost的使用。

继续使用WidgetSample工程的advancedviewsample模块,在app/main/res/layout/目录下创建tabhosttab_layout.xml文件,在其中填充如下代码片段:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@android:id/tabhost"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <LinearLayout
                android:id="@+id/widget_layout_red"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ff0000"
                android:orientation="vertical"/>
            <LinearLayout
                android:id="@+id/widget_layout_green"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00ff00"
                android:orientation="vertical"/>
            <LinearLayout
                android:id="@+id/widget_layout_blue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0000ff"
                android:orientation="vertical"/>
        </FrameLayout>
    </LinearLayout>
</TabHost>

请注意上面的布局文件中代码,从上面的布局文件可以发现,TabHost容器内部需要组合两个组件:TabWidget和FrameLayout,其中TabWidget用于定义选项卡的标题条, FrameLayout则用于层叠组合多个选项页面。不仅如此,上面的布局文件中这三个组件的 ID也有要求。

  • TabHost 的 ID 应该为@android:id/tabhost。
  • TabWidget 的 ID 应该为@android:id/tabs。
  • FrameLayout 的 ID 应该为@android:id/tabcontent。

上面这三个ID并不是开发者自己定义的,而是引用了 Android系统已有的ID。

接下来主程序即可加载该布局资源,并将布局文件中的三个Tab页面添加到该TabHost 容器中。新建TabHostTabActivity.java文件,加载上面新建的布局文件,具体代码如下:

package com.jinyu.cqkxzsxy.android.advancedviewsample;


import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;


/**
 * @创建者 鑫鱻
 * @描述 Android零基础入门到精通系列教程,欢迎关注微信公众号ShareExpert
 */
public class TabHostTabActivity extends TabActivity {
    private TabHost mTabHost = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhosttab_layout);


        // 从TabActivity上面获取放置Tab的TabHost
        mTabHost = getTabHost();


        // 添加第一个标签
        TabHost.TabSpec tab1 = mTabHost.newTabSpec("0");
        tab1.setIndicator("红色");
        tab1.setContent(R.id.widget_layout_red);
        mTabHost.addTab(tab1);


        // 添加第二个标签
        mTabHost.addTab(mTabHost
                // 创建新标签
                .newTabSpec("1")
                // 设置标签标题
                .setIndicator("绿色")
                // 设置该标签的布局内容
                .setContent(R.id.widget_layout_green));


        // 添加第三个标签
        mTabHost.addTab(mTabHost.newTabSpec("2")
                .setIndicator("蓝色")
                .setContent(R.id.widget_layout_blue));
    }
}

上面程序中的代码就是为TabHost创建并添加Tab页面的代码。上面的程序一共添加了三个标签页,用了两种方式来实现。

修改启动的Activity,运行程序,可以看到下图所示界面效果。

点击标签,可以切换显示的内容。

上面的程序调用了 TabHost.TabSpec对象的 setContent(int viewld)方法来设置标签页内容。

除此之外,还可调用setContent(Intent intent)方法来设置标签页内容,Intent还可用于启动其他Activity——这意味着TabHost.TabSpec可直接装载另一个Activity。

三、继承Activity实现

与继承TabActivity实现TabHost大体步骤差不多,唯一的区别就是没有TabActivity系统封装,必须由开发者自己获取TabHost组件而已。

在上面的示例基础上进行修改,创建tabhost_layout.xml文件,在其中填充如下代码片段:

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <LinearLayout
                android:id="@+id/widget_layout_red"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ff0000"
                android:orientation="vertical"/>
            <LinearLayout
                android:id="@+id/widget_layout_green"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00ff00"
                android:orientation="vertical"/>
            <LinearLayout
                android:id="@+id/widget_layout_blue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0000ff"
                android:orientation="vertical"/>
        </FrameLayout>
    </LinearLayout>
</TabHost>

从上述代码可以发现,除了TabHost 的id可以自定义外,TabWidget和FrameLayout仍然必须为系统的ID。

界面交互代码稍微有所不同,新建TabHostActivity.java文件,加载上面新建的布局文件,具体代码如下:

package com.jinyu.cqkxzsxy.android.advancedviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TabHost;


/**
 * @创建者 鑫鱻
 * @描述 Android零基础入门到精通系列教程,欢迎关注微信公众号ShareExpert
 */
public class TabHostActivity extends AppCompatActivity {
    private TabHost mTabHost = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhost_layout);


        // 获取TabHost组件
        mTabHost = (TabHost) findViewById(R.id.mytabhost);

        //调用 TabHost.setup()
        mTabHost.setup();


        // 添加三个标签
        mTabHost.addTab(mTabHost.newTabSpec("0").setIndicator("红色").setContent(R.id.widget_layout_red));;
        mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator("绿色").setContent(R.id.widget_layout_green));
        mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator("蓝色").setContent(R.id.widget_layout_blue));
    }
}

会发现除了查找我们自定义的TabHost组件外,还多了一条语句setup()来加载TabHost,否则程序会报错。

运行修改后的程序,最终效果同继承TabActivity一样。

有木有发现这个界面很不美观,所以在实际开发中经常会借用RadioButton来定制TabHost。

其实TabHost组件在安卓4.0之后已经被废弃了,建议使用Fragment组件来代替它。由于其设计违反了Activity单一窗口原则,它可以同时加载多个Activity,然后再它们之间进行来回切换;另外有个很致命的问题就是当点击别的选项时,按下Back后退键,它会使整个应用程序都退出,而不是切换到前一个选项卡,虽然可以在主程序里覆写OnKeyDown这个方法,但这样就会导致每一次按下Back后退键都只能回到第一个选项菜单。