TabHost的用法

今天学习了TabHost组件的功能和用法,在这里将Demos展现一下。

activity_tab_host_demos.xml

<TabHost
    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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="lzl.edu.com.windowprogressbar.TabHostDemos">

    <LinearLayout
        android:id="@+id/tab01"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="lzl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:text="lzl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/tab02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:text="lzf"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:text="lzf"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/tab03"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="wdw"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:text="wdw"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</TabHost>

TabHostDemos.java

package lzl.edu.com.windowprogressbar;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class TabHostDemos extends TabActivity {

    /**
     *本例主要讲解TabHost的用法:
     * TabHost仅仅是一个简单的容器,提供两个方法
     * newTabSpec(String tag):创建选项卡
     * addTab(TabHost.TabSpec tabSpec):添加选项卡
     * @param savedInstanceState
     *
     * 步骤:
     * 1、在界面布局中定义TabHost组件,并为该组件定义该选项卡内容
     * 2、Activity应该继承TabActivity。
     * 3、调用TabActivity的getTabhost()方法获取tabHost对象
     * 4、通过tabHost对象的方法来创建选项卡、添加选项卡。
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //调用TabActivity的getTabhost()方法获取tabHost对象
        TabHost tabHost = getTabHost();
        //添加tabHost布局
        LayoutInflater.from(this).inflate(R.layout.activity_tab_host_demos,
                tabHost.getTabContentView(),true);
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("已结电话").setContent(R.id.tab01));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("未接电话").setContent(R.id.tab02));
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("呼出电话").setContent(R.id.tab03));
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 15:11:10

TabHost的用法的相关文章

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

TabHost的用法(转)

本文结合源代码和实例来说明TabHost的用法.      使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图:       查看tabhost的源代码,主要实例变量有: private TabWidget mTabWidget;    private FrameLayout mTabContent;    private List<TabSpec> mTabSpecs 也就是说我们的tabhost必须有这三个东西,所以我们的.xml文件就会有规定:

Tabhost以及其用法

TabHost是一种非常使用的组件,TabHost可以方便的在窗口上放置多个标签页,每个标签页相当于或得了一个与外部容器相同大小的组件摆放区域. 与TabHost 结合使用的组件 TabWidget:代表选项卡的标签条. TabSpec:代表选项卡的一个Tab页面. 创建添加选项卡的方法: newTabSpec():创建选项卡 addTab():添加选项卡 使用TabHost的步骤: 在界面布局中定义TabHost组件 Activity继承TabActivity 调用TabActivity的ge

TabHost用法

tabhost用两种方法 方法一:Activity继承TabActivity后用getTabHost()方法来获取tabhost(前提:Activity的setContentView要删除,这样布局才能用TabActivity的默认布局,这样TabActivity的setup方法才不会出错),获取完后再对tabhost布局进行设置 方法二:用findViewById方法来获取tabhost,只是findViewById里的布局xml要符合一以下的格式 <?xml version="1.0&

Fragment + TabHost + RadioGroup

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法, Demo 效果图: 先上布局XML R.layout.activity_main [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"

tabhost使用

Tabhost用法 使用方法一:使用同一个布局文件 在xml中如此定义tabhost: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heigh

Android零基础入门第70节:ViewPager轻松完成TabHost效果

上一期学习了ViewPager的简单使用,本期一起来学习ViewPager的更多用法. 相信很多同学都使用过今日头条APP吧,一打开主界面就可以看到顶部有很多Tab,然后通过左右滑动来切换,就可以通过ViewPager来完成.当然具体实现又会有很多方式,我们本期就先来学习最简单的Tab切换吧,有一点类似于之前了解的TabHost. 一.PagerTitleStrip与PagerTabStrip 在实际运用中,很多时候只有页面滑动是不够的,还需要有标题栏才够友好.首先来学习一下官方自带的,在and

实现360手机助手TabHost的波纹效果

现在新版360手机助手的界面都做得挺漂亮的,在切换底部导航时的波纹效果也很好看,刚好最近看了个开源项目才了解到原来Drawable做动画效果也怎么好用,所以就仿照360实现了下带波纹的TabHost.源代码地址:https://github.com/Rukey7/XFragmentTabHost 先来看一下实现后的效果: 说明一下实现要点: 1. 因为我们项目之前用的是FragmentTabHost,所以我直接继承FragmentTabHost来实现动画效果更方便: 2. 波纹动画的实现其实是自

Android乐学成语之TabHost介绍及使用Fragment 替换 TabActivity

TabHost介绍 TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计; 1. TabHost常用组件 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加一个TabSpec即可添加到TabHost中; -- 创建选项卡 : newTabSpec(String tag), 创建一个选项卡; -- 添加选项卡 : addTab(tabSpec); TabHost的使用请看