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. PM2常用命令

    安装pm2 npm install -g pm2 1.启动 pm2 start app.js pm2 start app.js --name my-api    #my-api为PM2进程名称 pm2 ...

  2. DB2中横表纵表互换

    1.列转行:创建一个如下的表drop table dwtmp.tmp_xn_lsb; create table dwtmp.tmp_xn_lsb ( year      int ,quarter   ...

  3. WPF学习04:2D绘图 使用Shape绘基本图形

    我们将使用Shape进行基本图形绘制. 例子 一个可移动的矩形方框: XAML代码: <Window x:Class="Shape.MainWindow" xmlns=&qu ...

  4. systemback-----做你折腾的后盾

    http://imcn.me/html/y2015/24421.html ubuntu的系统还原工具,最近在学习grunt,要安装nodejs 等一些依赖,对于有轻微系统洁癖的我来说是个好的解决方案, ...

  5. WdatePicker时间插件 有百度云下载 jsp界面选择时间的简单方法

      链接:https://pan.baidu.com/s/1XCod602gCMv-qMQ4fMOLbg 提取码:ok8i 复制这段内容后打开百度网盘手机App,操作更方便哦 把东西复制到项目.导入j ...

  6. vs2010调试sql2008存储过程

      1.安装vs2010sp1补丁 2.vs中打开服务器资源管理器,并进行数据库连接,连接时要注意 3. 4.可以打开数据库中的存储过程进行调试了

  7. MySQL常用函数使用示例

    #从指定字符中,随机生成12位字符select substring('0123456789abcdefghijklmnopqrstuvwxyz',floor(0+RAND()*36),12); #显示 ...

  8. mac ssd开启trim模式

    开启方法 sudo trimforce enable

  9. OpenRead方法打开文件并读取

    实现效果: 知识运用: File类的OpenRead方法 //实现打开现有文件以进行读取 public static FileStream OpenRead(string path) FileStre ...

  10. Mybatis generator自动生成代码包括实体,dao,xml文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...