TabHost

TabHost选项卡

TabHost标签,TabWidget标签,FragmentLayout标签,这三个标签的id都是固定的系统定义的。

TabHost由标签(tabs)和标签体(tabcontent)组成。

 

是一个容器,可以包含并且运行多个嵌入的activity

已过时,推荐使用Fragment替代。

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <TabHost

        android:id="@android:id/tabhost"

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

 

        <TabWidget

            android:id="@android:id/tabs"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" >

        </TabWidget>

 

        <FrameLayout

            android:id="@android:id/tabcontent"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" >

        </FrameLayout>

    </TabHost>

 

</RelativeLayout>

 

创建TabHost步骤:

1.获取当前activity中的TabHostgetTabHost

2.创建一个标签TabSpec对象,它里面封装了标签指示符(卡片)和标签体(显示内容),指定标签的详细说明TabSpec TabSpec用来指定如何创建标签指示符和标签卡内容。tabHost.addTab(tabspec); TabSpec = TabHost.newTabSpace(tabname);

3.设置标签指示符和标签内容,标签内容是一个意图对象,用来开启一个activity

4.将标签TabSpec添加到TabHost选项卡中。

 

---------------------------代码示例-----------------

1.标签卡xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <TabHost

        android:id="@android:id/tabhost"

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

 

        <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" >

        </FrameLayout>

    </TabHost>

 

</RelativeLayout>

 

2.TabActivity

package com.itheima.tabhost;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.TabActivity;

import android.content.Intent;

import android.view.Menu;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

 

public class MainActivity extends TabActivity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 

TabHost tabHost = getTabHost();

 

TabSpec tab1 = tabHost.newTabSpec("标签1");

TabSpec tab2 = tabHost.newTabSpec("标签2");

TabSpec tab3 = tabHost.newTabSpec("标签3");

 

tab1.setIndicator("功能1");

tab1.setContent(new Intent(this,activity01.class));

 

tab2.setIndicator("功能2");

tab2.setContent(new Intent(this,activity02.class));

 

tab3.setIndicator("功能3");

tab3.setContent(new Intent(this,activity03.class));

 

tabHost.addTab(tab1);

tabHost.addTab(tab2);

tabHost.addTab(tab3);

}

 

}

3.activity01

package com.itheima.tabhost;

 

import android.app.Activity;

import android.os.Bundle;

 

public class activity01 extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

setContentView(R.layout.tab1_layout);

}

}

 

4.activity01xml布局文件

 

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

     >

 

   <TextView

       android:gravity="center"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:textSize="20sp"

        android:text="我是标签1" />

 

</RelativeLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值