<TabHost> 简单使用

本文详细介绍TabHost组件在Android应用中的使用方法,包括如何创建Tab页面、设置标题和内容等。通过示例代码展示了如何实现多标签页切换效果。

注意该类继承的是 TabActivity


package com.crazyit.ui.tabhostdemo;

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

/**
 * TabHost的 使用
 */
public class TabHostActivity extends TabActivity {

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

        //获取该 activity里面的 TabHost
         TabHost tabHost = getTabHost();

        //创建第一个 tab页
        TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1").
                setIndicator("已接电话")//设置标题
                .setContent(R.id.tab01); //设置内容
        //添加第一个 tab 标签页
        tabHost.addTab(tab1);

        TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2")
                .setIndicator("呼出电话",getResources().getDrawable(R.mipmap.ic_launcher))
                .setContent(R.id.tab02);
        //添加第2个 tab标签页
        tabHost.addTab(tab2);

        //创建第3个标签页
        TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3")
                .setIndicator("已接电话")
                .setContent(R.id.tab03);
        //添加 第3个标签页
        tabHost.addTab(tab3);
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"   //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:context="com.crazyit.ui.tabhostdemo.TabHostActivity">

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

        <!-- tab标题-->
        <TabWidget
            android:id="@android:id/tabs" //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent" //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!--定义第一个标签页的内容-->
            <LinearLayout
                android:id="@+id/tab01"  android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="八部天龙"
                    android:textSize="22dp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="东海三太子"
                    android:textSize="22dp" />
            </LinearLayout>
            <!--定义第2个标签页的内容-->
            <LinearLayout
                android:id="@+id/tab02"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="斗战圣佛"
                    android:textSize="22dp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="花果山水帘洞"
                    android:textSize="22dp" />
            </LinearLayout>
            <!--定义第3个标签页的内容-->
            <LinearLayout
                android:id="@+id/tab03"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="净坛使者"
                    android:textSize="22dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="天蓬元帅"
                    android:textSize="22dp" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
  1. TabHost容器 需要两个组件 TabWidget和 Framelayout
  2. FrameLayout 用于 层叠组合多个选项卡
  3. 布局文件的 id也是有要求的
  4. TabHost 的 ID 为@android:id/tabhost
  5. TabWidget的 ID为 @android:id/tabs
  6. FrameLayout的 Id @android:id/tabcontent
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值