<TabHost> 简单使用

来源:互联网 发布:手机淘宝评价怎么删 编辑:程序博客网 时间:2024/05/17 08:06

注意该类继承的是 TabActivity


package com.crazyit.ui.tabhostdemo;import android.app.TabActivity;import android.os.Bundle;import android.widget.TabHost;/** * TabHost的 使用 */public class TabHostActivity extends TabActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_tab_host);        //获取该 activity里面的 TabHost         TabHost tabHost = getTabHost();        //创建第一个 tab页        TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1").                setIndicator("已接电话")//设置标题                .setContent(R.id.tab01); //设置内容        //添加第一个 tab 标签页        tabHost.addTab(tab1);        TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2")                .setIndicator("呼出电话",getResources().getDrawable(R.mipmap.ic_launcher))                .setContent(R.id.tab02);        //添加第2个 tab标签页        tabHost.addTab(tab2);        //创建第3个标签页        TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3")                .setIndicator("已接电话")                .setContent(R.id.tab03);        //添加 第3个标签页        tabHost.addTab(tab3);    }}

布局文件

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@android:id/tabhost"   //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_weight="1"    tools:context="com.crazyit.ui.tabhostdemo.TabHostActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <!-- tab标题-->        <TabWidget            android:id="@android:id/tabs" //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <FrameLayout            android:id="@android:id/tabcontent" //注意此处的 id 是引用Android系统自己的 不是开发人员自己定义的            android:layout_width="match_parent"            android:layout_height="match_parent">            <!--定义第一个标签页的内容-->            <LinearLayout                android:id="@+id/tab01"  android:gravity="center"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="八部天龙"                    android:textSize="22dp" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="东海三太子"                    android:textSize="22dp" />            </LinearLayout>            <!--定义第2个标签页的内容-->            <LinearLayout                android:id="@+id/tab02"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="斗战圣佛"                    android:textSize="22dp" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="花果山水帘洞"                    android:textSize="22dp" />            </LinearLayout>            <!--定义第3个标签页的内容-->            <LinearLayout                android:id="@+id/tab03"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="净坛使者"                    android:textSize="22dp" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="天蓬元帅"                    android:textSize="22dp" />            </LinearLayout>        </FrameLayout>    </LinearLayout></TabHost>
  1. TabHost容器 需要两个组件 TabWidget和 Framelayout
  2. FrameLayout 用于 层叠组合多个选项卡
  3. 布局文件的 id也是有要求的
  4. TabHost 的 ID 为@android:id/tabhost
  5. TabWidget的 ID为 @android:id/tabs
  6. FrameLayout的 Id @android:id/tabcontent
0 0