TabHost的两种使用方式

本文介绍如何使用 TabHost 在 Android 中实现不同标签间的布局切换和 Activity 切换。包括直接切换布局的方法及通过 Intent 切换 Activity 的两种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.TabHost实现不同的标签之间切换时切换不同的布局</span>

1)实现一个继承自TabActivity的Activity

2)定义一个Activity对应的布局文件,在布局文件中定义各个标签对应的布局。布局文件可以以TabHost为根标签,可以以LinearLayout或其他布局管理器为根标签。

布局文件的代码示例:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/host"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundcolor"
     >
	<LinearLayout android:id="@+id/tab01"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:orientation="horizontal">
	    <TextView 
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:text="第一个标签"
	        android:textColor="@android:color/black"/>
	</LinearLayout>
	
	<LinearLayout android:id="@+id/tab02"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:orientation="horizontal">
	    <TextView 
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:text="第一个标签"
	        android:textColor="@android:color/darker_gray"/>
	</LinearLayout>
	
	<LinearLayout android:id="@+id/tab03"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:orientation="horizontal">
	    <TextView 
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:text="第一个标签"
	        android:textColor="@android:color/primary_text_dark_nodisable"/>
	</LinearLayout>
</TabHost>
在布局文件中定义了具体Tab的内容。

3)在Activity中先声明TabHost对象,然后使用LayoutInflater过滤出布局文件。不能使用this.setContentView(R.layout.tab_demo),使用报异常。

tabhost=getTabHost();
LayoutInflater.from(this).inflate(R.layout.tab_demo, tabhost.getTabContentView(), true);
4)在TabHost中创建添加标签

tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("标签一")
				.setContent(R.id.tab01));
		tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("标签二")
				.setContent(R.id.tab02));
		tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("标签三")
				.setContent(R.id.tab03));*

2.TabHost实现不同的标签之间切换时实现不同Activity之间的切换

实现方式一:

1)实现一个继承自TabActivity的Activity

2)定义一个布局文件,根标签为TabHost,TabHost中没有定义标签对应的布局文件

3)在Activity中,直接用Intent作为标签的内容实现点击标签跳转到另一个Activity

Intent togroup=new Intent(ContactActivity.this,GroupActivity.class);
        Intent topeople=new Intent(ContactActivity.this,PeopleActivity.class);
        Intent tostore=new Intent(ContactActivity.this,StoreActivity.class);
        TabHost host=this.getTabHost();
        LayoutInflater.from(this).inflate(R.layout.activity_contact,host.getTabContentView(),true);
        host.addTab(host.newTabSpec("group").setIndicator("分组").setContent(togroup));
        host.addTab(host.newTabSpec("people").setIndicator("联系人").setContent(topeople));
        host.addTab(host.newTabSpec("store").setIndicator("收藏").setContent(tostore));

实现方式二:

1)实现一个继承自TabActivity的Activity

2)定义一个布局文件,布局文件中TabHost中必须要有一个FrameLayout和一个TabWiget,FrameLayout的id必须为android:id="@android:id/tabcontent",TabWiget的id必须为android:id="@android:id/tabs",TabHost的id必须为android:id="@android:id/tabhost"

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroundcolor"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
       <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

         <ImageView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/shallowgray"
            android:layout_marginBottom="5dp"
         /> 
         
   	    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dp"
            android:layout_weight="0"
            android:orientation="horizontal"
             >
               
             <LinearLayout
                android:id="@+id/linearlayout_groupbuy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/img_groupbuy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:background="@drawable/ic_menu_deal_on" />

                <TextView
                    android:id="@+id/text_groupbuy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/mian_bottom_group_buy"
                    android:textColor="@color/green"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearlayout_merchant"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/img_merchant"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:background="@drawable/ic_menu_poi_off" />

                <TextView
                    android:id="@+id/text_merchant"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/mian_bottom_merchant"
                    android:textColor="@color/textgray"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearlayout_mine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/img_mine"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:background="@drawable/ic_menu_user_off" />

                <TextView
                    android:id="@+id/text_mine"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/mian_bottom_meself"
                    android:textColor="@color/textgray"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearlayout_more"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/img_more"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:scaleType="matrix"
                    android:background="@drawable/ic_menu_more_off" />

                <TextView
                    android:id="@+id/text_more"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/mian_bottom_more"
                    android:textColor="@color/textgray"
                    android:textSize="12sp" />
            </LinearLayout>

            
        </LinearLayout>
      	<TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:visibility="gone" />  
    </LinearLayout>

</TabHost>
使用这种方式定义布局文件之后,可以直接使用this.setContentView(R.layout.main)来加载布局文件,可以显示正常的布局但是点击标签时没有反应。

3)添加标签页的具体内容:

TabSpec tabSpec = host.newTabSpec(GROUPBUY_STRING);
tabSpec.setIndicator(GROUPBUY_STRING);
Intent intent = new Intent(MainActivity.this, GroupBuyActivity.class);
tabSpec.setContent(intent);
host.addTab(tabSpec);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值