第14天Tabhost了解

Tabhost

一.效果:和TabLayout的效果一样,但是已经过时,简单了解

在这里插入图片描述

二.思路

方式一: 继承 Activity

			布局页面中:
			
					1, 跟标签 : <TabHost/>  id 任意指定
					2, <TabWidget/>			id设置:  @android:id/tabs
					3, FrameLayout			id 设置: @android:id/tabcontent

方式二: 继承TabActivity

			布局页面中:

					1, 根标签: <TabHost/>   id设置: @android:id/tabhost

					2, <TabWidget/>			id设置: @android:id/tabs

					3, FrameLayout			id 设置:@android:id/tabcontent

三.代码

(1)布局activity_main.xml:TabHost/TabWidget/FrameLayout三个标签必须有

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    >

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

        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@android:id/tabs"
            />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent"
            >

            <!--第一个页面-->
            <LinearLayout
                android:id="@+id/layout_01"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="第一个页面"
                    />
            </LinearLayout>


            <!--第二个页面-->
            <LinearLayout
                android:id="@+id/layout_02"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="第二个页面"
                    />
            </LinearLayout>


            <!--第三个页面-->
            <LinearLayout
                android:id="@+id/layout_03"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="第三个页面"
                    />
            </LinearLayout>
        </FrameLayout>

    </LinearLayout>

</TabHost>

(3)Java代码:MainActivity.java

//TODO 1, Activity 继承TabActivity
public class MainActivity extends TabActivity {

    private TabHost mTabHost;

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

        //TODO 2, 获取TabHost
        mTabHost = getTabHost();

        //TODO 3, 添加选项卡
        mTabHost.addTab(mTabHost.newTabSpec("tag1")//设置标签
                                .setIndicator("新闻")//设置标题
                                .setContent(R.id.layout_01)//设置显示的内容
                        );

        mTabHost.addTab(mTabHost.newTabSpec("tag2")//设置标签
                .setIndicator("娱乐")//设置标题
                .setContent(R.id.layout_02)//设置显示的内容
        );


        mTabHost.addTab(mTabHost.newTabSpec("tag3")//设置标签
                .setIndicator("体育")//设置标题
                .setContent(R.id.layout_03)//设置显示的内容
        );
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值