android TabHost(选项卡)

1:在布局文件中配置选项卡的内容

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- 定义第一个标签页的内容 -->
            <LinearLayout
                android:id="@+id/tab01"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女儿国国王 - 2012/12/12"
                    android:textSize="11pt" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="东海龙女 - 2012/12/18"
                    android:textSize="11pt" />
            </LinearLayout>
            <!-- 定义第二个标签页的内容 -->
            <LinearLayout
                android:id="@+id/tab02"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="白骨精  - 2012/08/12"
                    android:textSize="11pt" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="蜘蛛精 - 2012/09/20"
                    android:textSize="11pt" />
            </LinearLayout>
            <!-- 定义第三个标签页的内容 -->
            <LinearLayout
                android:id="@+id/tab03"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textSize="11pt">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="孙悟空 - 2012/09/19"
                    android:textSize="11pt" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="猪八戒  - 2012/10/12"
                    android:textSize="11pt" />
                <Button 
                    android:id="@+id/btn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="确定"/>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

响应的代码

package org.crazyit.ui;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class TabHostTest extends TabActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 获取该Activity里面的TabHost组件
        TabHost tabHost = getTabHost();
        // 创建第一个Tab页
        TabSpec tab1 = tabHost.newTabSpec("tab1")
            .setIndicator("已接电话") // 设置标题
            .setContent(R.id.tab01); //设置内容
        // 添加第一个标签页
        tabHost.addTab(tab1);
        TabSpec tab2 = tabHost.newTabSpec("tab2")
            // 在标签标题上放置图标
            .setIndicator("呼出电话", getResources()
            .getDrawable(R.drawable.ic_launcher))
            .setContent(R.id.tab02);
        // 添加第二个标签页
        tabHost.addTab(tab2);
        TabSpec tab3 = tabHost.newTabSpec("tab3")
            .setIndicator("未接电话")
            .setContent(R.id.tab03);
        // 添加第三个标签页
        tabHost.addTab(tab3);
        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();
                
            }
        });
    }
}

2:选项卡的内容通过不同的布局文件配置

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </LinearLayout>
</TabHost>

设置选项卡的activity

package org.crazyit.intent;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class IntentTab extends TabActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 获取该Activity里面的TabHost组件
        TabHost tabHost = getTabHost();
        // 使用Intent添加第一个Tab页面

     //BacallActivity ,CallActivity,NoCallActitity为三个activity,控制显示选项卡的内容
        tabHost.addTab(tabHost
            .newTabSpec("tab1")
            .setIndicator("已接电话",
                getResources().getDrawable(R.drawable.ic_launcher))
            .setContent(new Intent(this, BeCalledActivity.class)));
        // 使用Intent添加第二个Tab页面
        tabHost.addTab(tabHost.newTabSpec("tab1")
            .setIndicator("呼出电话")
            .setContent(new Intent(this, CalledActivity.class)));
        // 使用Intent添加第三个Tab页面
        tabHost.addTab(tabHost.newTabSpec("tab1")
            .setIndicator("未接电话")
            .setContent(new Intent(this, NoCallActivity.class)));
    }
}
原文地址:https://www.cnblogs.com/songyao/p/4076762.html