Android Tab简介

 TabHost是tab窗口的容器。一个TabHost对象包含两个子元素:一个对象是tab集合(TabWidget),用户点击它们来选择一个特定的tab项;另一个是FrameLayout对象,展示当前tab的内容。子元素通常是通过TabHost容器对象来控制,而不是直接设置子元素的值。实现方式有继承TabActivity、继承Activity类两种。

1. 从TabActivity中用getTabHost()方法获取TabHost,然后设置标签内容:

 <?xml version="1.0" encoding="utf-8"?>

<!-- 各个Tab项的内容部分  -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
    android:layout_height="fill_parent">

	<LinearLayout android:id="@+id/widget_layout_Blue"
		android:layout_width="fill_parent"
        android:layout_height="fill_parent"
		android:orientation="vertical"
        android:gravity="center"
        android:background="@drawable/back_blue" >
		<Button android:id="@+id/to_tab2_demo"
            android:layout_width="wrap_content"
			android:layout_height="wrap_content"
            android:text="Button2" />

        <Button android:id="@+id/to_tab3_demo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3"/>

        <Button android:id="@+id/to_tab4_demo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button4" />
	</LinearLayout>

	<LinearLayout android:id="@+id/widget_layout_green"
		android:layout_width="fill_parent"
        android:layout_height="fill_parent"
		android:orientation="vertical"
        android:gravity="center"
        android:background="@drawable/back_red" >
		<AnalogClock android:id="@+id/widget36"
			android:layout_width="wrap_content"
            android:layout_height="wrap_content">
		</AnalogClock>
	</LinearLayout>

	<LinearLayout android:id="@+id/widget_layout_red"
		android:layout_width="fill_parent"
        android:layout_height="fill_parent"
		android:orientation="vertical"
        android:gravity="center"
        android:background="@drawable/back_green" >
		<RadioGroup android:id="@+id/widget43"
			android:layout_width="166px"
            android:layout_height="98px"
			android:orientation="vertical">
			<RadioButton android:id="@+id/widget44"
				android:layout_width="wrap_content"
                android:layout_height="wrap_content"
				android:text="RadioButton">
			</RadioButton>
			<RadioButton android:id="@+id/widget45"
				android:layout_width="wrap_content"
                android:layout_height="wrap_content"
				android:text="RadioButton">
			</RadioButton>
		</RadioGroup>
	</LinearLayout>

</FrameLayout>


2. 继承Activity实现方式:

   TabHost    必须设置android:id为@android:id/tabhost

           TabWidget   必须设置android:id为@android:id/tabs

            FrameLayout   必须设置android:id为@android:id/tabcontent


<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><?xml version="1.0" encoding="utf-8"?></span>
<pre name="code" class="html"><!-- 各个Tab项的内容部分  -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    <span style="color:#ff0000;">android:id="@android:id/tabhost"</span>
	android:layout_width="match_parent"
    android:layout_height="match_parent">

	<LinearLayout android:id="@+id/widget_layout_Blue"
		android:layout_width="match_parent"
        android:layout_height="match_parent"
		android:orientation="vertical"
        android:background="@drawable/back_blue" >

        <TabWidget
            <span style="color:#ff0000;">android:id="@android:id/tabs"</span>
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            <span style="color:#ff0000;">android:id="@android:id/tabcontent"</span>
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <LinearLayout android:id="@+id/view1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:gravity="center"
                android:background="@drawable/back_red" >
                <AnalogClock android:id="@+id/widget36"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                </AnalogClock>
            </LinearLayout>

            <LinearLayout android:id="@+id/view2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:gravity="center"
                android:background="@drawable/back_green" >
                <RadioGroup android:id="@+id/widget43"
                    android:layout_width="166px"
                    android:layout_height="98px"
                    android:orientation="vertical">
                    <RadioButton android:id="@+id/widget44"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="RadioButton">
                    </RadioButton>
                    <RadioButton android:id="@+id/widget45"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="RadioButton">
                    </RadioButton>
                </RadioGroup>
            </LinearLayout>
        </FrameLayout>
        
    </LinearLayout>

</TabHost>


 


 其实,通过两种方式发现区别并不大,继承TabActivity的方式就是系统已经有了关于tabhost的布局了,自己可以只负责显示内容部分和建立相应的tab项。


项目源码:http://download.csdn.net/detail/linghu_java/8440471(android studio项目)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值