TabHost

1、选项卡是一种能重复利用屏幕空间的实用组件
    一屏多用
    功能分组

2、组件名
    TabHost
        继承自FrameLayout,代表选项卡宿主容器
        Activity的父类必须是TabActivity
    TabWidget
        代表选项卡标签栏
    TabSpec

        代表选项卡标签

 TabHost的结构




3、布局文件结构
    <TabHost android:id="@android:id/tabhost">
       <LinearLayout>
      <!--选项卡导航区-->
          <TabWidget android:id="@android:id/tabs">
          </TabWidget>
    
    <!--内容区-->
        <FrameLayout android:id="@android:id/tabcontent">
        </FrameLayout>
      </LinearLayout>
</TabHost>

注意:其中布局是不能变的,id也是固定的,getTabHost对象就是通过id来获取的

4、TabHost优点:
    实现简单,不用手动添加各类监听
   TabHost缺点:
    不支持滑动
    所有的页面文件都在放在同一个布局文件中,
        维护和扩展都不方便,布局文件也会变得很庞大
案例:
TabHost的结构布局文件
    内部是一个上下结构的线性布局,上端是TabWidget,
    下部是一个帧布局的内容区


案例效果图


<?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"
    android:orientation="vertical" >
    
    <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"
            >
        </TabWidget>
        
        <!-- 内容区 -->
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            
            <!-- 评论区 -->
            <LinearLayout
                android:id="@+id/tabhost_comment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FF0000"
                android:orientation="vertical"
                ></LinearLayout>
            
            <!-- 转发区 -->
            <LinearLayout
                android:id="@+id/tabhost_transmit"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFF00"
                android:orientation="vertical"
                ></LinearLayout>
            
            
            <!-- 私信区 -->
            <LinearLayout
                android:id="@+id/tabhost_message"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FF00FF"
                android:orientation="vertical"
                ></LinearLayout>
        
        </FrameLayout>
    </LinearLayout>

</TabHost>


Activity活动的代码
package com.hngd.tabhost;

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

public class TabHostActivity extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabhost);
        //通过内置的 android:id="@android:id/tabhost"获取到TabHost
        TabHost tabHost = getTabHost();
        {
            //newTabSpec()中传入的字符串a,是一个Tag标记,可以通过
            //它来区分不同的TabSpec
            TabHost.TabSpec tabSpec = tabHost.newTabSpec("a");
            tabSpec.setIndicator("评论");
            //关联内容区
            tabSpec.setContent(R.id.tabhost_comment);
            tabHost.addTab(tabSpec);
        }
        
        
        {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec("b");
            tabSpec.setIndicator("转发");
            tabSpec.setContent(R.id.tabhost_transmit);
            tabHost.addTab(tabSpec);
        }
        
        {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec("c");
            tabSpec.setIndicator("私信");
            tabSpec.setContent(R.id.tabhost_message);
            tabHost.addTab(tabSpec);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值