TabHost的实现有两种方式,第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

mainActivity.xml

private TabHost myTabHost;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
       myTabHost = this.getTabHost();
        LayoutInflater.from(this).inflate(R.layout.main,
                myTabHost.getTabContentView(), true);
        myTabHost.addTab(myTabHost
                .newTabSpec("选项卡1")
                .setIndicator("选项卡1",
                        getResources().getDrawable(R.drawable.img01))
                .setContent(R.id.ll01));
        myTabHost.addTab(myTabHost
                .newTabSpec("选项卡2")
                .setIndicator("选项卡2",
                        getResources().getDrawable(R.drawable.img02))
                .setContent(R.id.ll01));
        myTabHost.addTab(myTabHost
                .newTabSpec("选项卡3")
                .setIndicator("选项卡3",
                        getResources().getDrawable(R.drawable.img03))
                .setContent(R.id.ll03));

}

Tab内容布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/ll01" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="center_horizontal"
        android:orientation="vertical">
        <EditText android:id="@+id/widget34" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="EditText"
            android:textSize="18sp">
        </EditText>
        <Button android:id="@+id/widget30" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Button">
        </Button>

</LinearLayout>
    <LinearLayout android:id="@+id/ll02" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="center_horizontal"
        android:orientation="vertical">
        <AnalogClock android:id="@+id/widget36"
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </AnalogClock>
    </LinearLayout>
    <LinearLayout android:id="@+id/ll03" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:gravity="center_horizontal"
        android:orientation="vertical">
        <RadioGroup android:id="@+id/widget43"
            android:layout_width="166px" android:layout_height="98px"
            android:orientation="vertical">
            <RadioButton android:id="@+id/widget44"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="RadioButton">
            </RadioButton>
            <RadioButton android:id="@+id/widget45"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="RadioButton">
            </RadioButton>
        </RadioGroup>

</LinearLayout>
</FrameLayout>

第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/hometabs"
    android:orientation="vertical"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"> 
    <TabHost android:id="@+id/tabhost"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content">
         <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            
             <TabWidget android:id="@android:id/tabs" 
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
            </TabWidget>
         
             <FrameLayout android:id="@android:id/tabcontent"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
                      <TextView android:id="@+id/view1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" android:text="Tab1"/>
                    <TextView android:id="@+id/view2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" android:text="Tab2"/>
                    <TextView android:id="@+id/view3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" android:text="Tab3"/>
             </FrameLayout>
         
         </LinearLayout>
    </TabHost>
</LinearLayout>

mainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        TabWidget tabWidget = tabHost.getTabWidget();

tabHost.addTab(tabHost
                .newTabSpec("tab1")
                .setIndicator("tab1",
                        getResources().getDrawable(R.drawable.img01))
                .setContent(R.id.view1));

tabHost.addTab(tabHost
                .newTabSpec("tab2")
                .setIndicator("tab2",
                        getResources().getDrawable(R.drawable.img02))
                .setContent(R.id.view2));

tabHost.addTab(tabHost
                .newTabSpec("tab3")
                .setIndicator("tab3",
                        getResources().getDrawable(R.drawable.img03))
                .setContent(R.id.view3));

android 之 TabHost的更多相关文章

  1. Android底部TabHost API

    今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...

  2. 12.Android之Tabhost组件学习

    TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...

  3. android之TabHost(下)

    首先建立res/layout/tab.xml文件 编写代码如下: <?xml version="1.0" encoding="utf-8"?> &l ...

  4. android之TabHost(上)

    首先建立文件res/layout/tab.xml 代码如下: <?xml version="1.0" encoding="utf-8"?> < ...

  5. Android:TabHost实现Tab切换

    TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...

  6. Android选项卡TabHost方式实现

    1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...

  7. android使用tabhost实现导航

    参考 http://blog.csdn.net/xixinyan/article/details/6771341 http://blog.sina.com.cn/s/blog_6b04c8eb0101 ...

  8. android的tabhost+RadioGroup+PopupWindow

    根据网上的代码稍作修改了下,放着记录学习. 效果图如下: 主代码如下: package com.andyidea.tabdemo; import android.app.TabActivity; im ...

  9. android学习--TabHost选项卡组件

    TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...

  10. Android学习Tabhost、gallery、listview、imageswitcher

    Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...

随机推荐

  1. Jackson 动态过滤属性,编程式过滤对象中的属性

    场景:有时候我们做系统的时候,比如两个请求,返回同一个对象,但是需要的返回字段并不相同. 常见与写前端接口的时候,尤其是手机端,一般需要什么数据就返回什么样的数据.此时对于返回同一个对象我们就要动态过 ...

  2. Oracle 恢复数据后,数据库中中文变成问号解决方法

    1.右击---我的电脑---环境变量 2.新增环境变量 变量名:LANG=zh_CN.GBK NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 3.重启PLSQL或 ...

  3. 从零开始利用vue-cli搭建简单音乐网站(六)

    上一篇遗漏了一个简单的效果没写,见下图: 主页面点击热门推荐和更多之后跳转到歌曲列表页面,现在的页面只是简单的把所有歌曲列出来,没有进行排序.实现起来也很简单,在MainPage的两个链接上添加: & ...

  4. java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/xxx/lib/arm/liblame.so: has text relocations

    最近在写本地录音转码过程中引入了liblame.so,我这边用了不同系统版本的手机测试本地录音都没有出现问题,但是有一天,同事在测试的时候,出现了以下错误: 09-13 17:32:29.140 26 ...

  5. 浅谈table和DIV网页布局

    DIV+CSS是网站标准(或称“WEB标准”)中常用的术语之一,通常为了说明与HTML网页设计语言中的表格(table)定位方式的区别,因为XHTML网站设计标准中,不再使用表格定位技术,而是采用DI ...

  6. 洛谷 P2922 [USACO08DEC]秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  7. openstack v3 rest 访问

    1. openstack主要面向得是python为主得开发.目前java中嵌入openstack主要是通过rest接口访问 2. 下载一个postman的接口测试工具 3. openstack 中的服 ...

  8. DRM 简介

    首先,我们对和DRM 相关的一些概念进行介绍. Buffer: 对于RAC 数据库,当一个数据块被读入到buffer cache后,我们就称其为buffer , cache fusion 会将这个bu ...

  9. CAD交互绘制云线批注(网页版)

    js中实现代码说明: 动态拖放时的绘制事件: function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) { //自定义实体的GUID标识符 var sGui ...

  10. C++判断两个double类型双精度浮点数是否同号

    看到的一种整数的方法 != y < ) 由此, double x,y; == fabs( ) { } 目前想到的比较合适判断方法. 此外这里还有一种强制转换类型求符号位的方法. /** * Ge ...