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 [email protected]
 * @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 [email protected]
 * @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)));
    }
}
时间: 2024-08-29 15:50:57

android TabHost(选项卡)的相关文章

android学习--TabHost选项卡组件

TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页获得了一个与外部容器相同大小的组件摆放区域.在手机系统的应用类似"未接电话"."已接电话"."呼出电话"等. 1 . TabHost提供了两个方法来创建选项卡.添加选项卡 newTabSpec(String tag)  : 创建选项卡 addTab(TabHost.TabSpec  tabSpec) : 添加选项卡 2.TabHost 切换选项卡触发的

修炼-------------Android TabHost,TabWidget选项卡总结

修炼-------------Android TabHost,TabWidget选项卡总结 Android之TabHost TabHost,个人理解为选项卡的容器,是一种特殊的FrameLayout布局(帧布局) 根据SDK文档, Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab,

Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡

 <Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePageIndicator(原文链接:http://blog.csdn.net/zhangphil/article/details/44752213),自己写了一个底部带有滑块.且当ViewPager页面切换时候选项卡也随之相应切换,且滑块也随之相应动态滑动效果得控件.但写的太过于紧耦合,不利于复用,所以现在重构

Android ——TabHost使用

在Android中,通常可以使用切换卡(选项卡)实现切换显示不同页面内容的功能.这一功能可以通过TabHost控件来实现. 下面我们就通过一个简单的实例演示如何使用TabHost控件完成切换卡功能,完成后的运行效果如图1所示. 图1 主页显示效果 可以看出,在该实例中,总共设置了四个TabHost标签,分别为主页.时间.联系人和搜索.在点击这些标签时,便可以完成相应页面内容的显示. 1.界面布局 TabHost是整个Tab的容器,是由TabWidget和FrameLayout 两部分组成的.其中

相当郁闷的问题,TabHost选项卡标签图标始终不出现?

在学习Android TabHost布局过程中,很多教程告诉我,这样来显示选项卡标签的图标和文字: TapSpec spec1 = tabHost.newTabSpec("tab 1"); spec1.setIndicator("选项卡一", getResources().getDrawable(R.drawable.tab_icon)); spec1.setContent(R.id.tab1); tabHost.addTab(spec1); 折腾来折腾去,setI

Android中选项卡功能的实现

Android中选项卡功能的实现 Android中使用TabHost和TabWidget来实现选项卡功能.TabHost必须是布局的根节点,它包含两个子节点: TabWidget,显示选项卡: FrameLayout,显示标签内容. 实现选项卡功能有两种方法,一种是将多个View放在同一个Activity中,然后使用使用标签来进行切换.另一种是直接使用标签切换不同的Activity. 后一种方法更为常用一些.本文也只介绍了后一种方法的实现过程. 1. 创建一个工程,名字可以叫HelloTabWi

Android -- TabHost、Fragment、状态保存、通信

工程结构                                                                                       TabAFm到TabEFm都是Fragment,并且每个Fragment对应一个布局文件. TabAFm.java                                                                             package com.yydcdut.tabho

【原创】android——Tabhost 自定义tab+底部实现+intent切换内容

1,实现tabhost自定义格式,再此仅仅显示背景和文字,效果图预览:(底边栏所示) (图片变形) 2,xml配置 activity_user的XML配置  1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/tabhost&qu

Android TabHost(简易用法)

前言 欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net 雨季o莫忧离:http://blog.csdn.net/luckkof 正文 Tab应用的结构 TabHost的Activity的结构如下: <?xml version="1.0" encoding="utf-8"?> <!-- 定义TabHost组件 --> <LinearLayout

Android tabhost下的activity怎样获取传来的值

android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); intent.putExtra("键",值);//intent键值对传的值 intent.setClass(FromActivity.this,TabHostActivity.class); FromActivity.this.startActivity(intent);tabhostactic