TabHost的使用及原理

本文详细介绍了Android中的TabHost组件,包括它的定义、使用前提、三种实现方式及其具体实现示例。重点讲解了如何通过TabSpec的setContent方法实现不同页面跳转,并探讨了TabHost的工作原理,解析了源码中的关键部分。同时,文章还解答了为何在某些情况下TabHost的选择卡中无法显示图片的问题。
摘要由CSDN通过智能技术生成

一、TabHost是什么

TabHost在文档中的定义是:Container for a tabbed window view.直译过来是一个选项卡式窗口视图容器。简单来说,就是类似于微信首页那种,有几个选择卡,点击选择卡可以跳到不同的页面。

二、在了解TabHost之前,有以下知识是需要了解的:

1、TabHost的xml文件中,TabHost、TabWidget和显示TabHost的部分(通常用FrameLayout),这3部分的id是不可修改的,必须使用Android提供的id。

2、TabHost在自定义前,需要初始化,也就是调用tab.setup();(tab是TabHost对象)

3、使用TabHost在几个activity间跳转时,需要继承AcitvityGroup类。并且在初始化时需要调用tab.setup(ActivityGroup.getLocalActivityManager());

4、TabSpec是TabHost的内部类,TabHost需要通过tab.addTab(TabSpec对象)来添加组件,添加的组件之间可以通过TabHost跳转。而实现跳转最重要的是TabSpec的setContent方法,简单来说就是TabSpec的setContent方法传入什么参数,决定TabHost会跳转到哪里。(如果这里不懂没有关系,讲原理的时候会加上源码,到时候就懂了)

三、TabHost有3种实现方式:(通过TabSpec的setContent方法实现)

1、第一种是把需要改变的布局全部写在TabHost的xml中,然后setContent传入id。

2、第二种是在几个Activity间跳转,setContent传入intent对象。

3、第三种是自己定义几种View,然后在这几种View之间跳转。setContent传入TabHost的内部类TabContentFactory的对象。需要重写TabContentFactory的抽象方法。

四、具体使用实现

1、第一种:把需要改变的布局全部写在TabHost的xml中
这种方法是将所有要跳转的布局全部写在TabHost控件中。在给TabHost的对象添加组件时,给TabSpec的setContent方法传入int类型的参数,参数的意义为:要跳转到的布局的id,这个布局为TabHost的子控件。示例如下:

tabspec_int.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- TabHost组件id值不可变-->
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <RelativeLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

       <!-- TabWidget组件id值不可变-->
       <TabWidget 
           android:id="@android:id/tabs"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true">
       </TabWidget>

       <!-- FrameLayout布局,id值不可变-->
       <FrameLayout 
           android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_above="@android:id/tabs">

           <!-- 第一个tab的布局 -->  
           <LinearLayout  
               android:id="@+id/tab1"  
               android:layout_width="match_parent"  
               android:layout_height="match_parent" >  

               <TextView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:text="第一个tab的布局" />  
           </LinearLayout>  

           <!-- 第二个tab的布局 -->  
           <LinearLayout  
               android:id="@+id/tab2"  
               android:layout_width="match_parent"  
               android:layout_height="match_parent" >  

               <TextView  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:text="第二个tab的布局" />  
           </LinearLayout>  

           <!-- 第三个tab的布局 -->  
           <LinearLayout  
               android:id="@+id/tab3"  
               android:layout_width="match_parent"  
               android:layout_height="match_parent" >  

               <TextView    
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:text="第三个tab的布局" />  
           </LinearLayout>  

       </FrameLayout>     
    </RelativeLayout>   
</TabHost>

上面的xml中,tabcontent中有3个tab的布局,id分别为tab1、tab2和tab3。在TabspecInt.java中,在给tab.addTab添加组件时,给TabSpec的setContent传入tab1、tab2和tab3的id。

TabspecInt.java

package com.example.tabtest;

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

public class TabspecInt extends Activity {
   

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

        TabHost tab = (TabHost) findViewById(android.R.id.tabhost);  

        //初始化TabHost容器
        tab.setup();              

        //在TabHost创建标签,然后设置:标题/图标/标签页布局  
        tab.addTab(ta
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值