职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 325|回复: 1

TabHost使用小结

[复制链接]
月上萧萧 发表于 2011-8-29 09:30 | 显示全部楼层 |阅读模式
使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用。
完成一个TabHost的步骤:
一、设计布局文件,Tabhost布局文件一般使用FrameLayout,在FrameLayout中添加每个Tab页面的视图,但必须要有id,例如:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">

<!-- 第一个Tab 对应的布局 -->
<!-- Tab内容必须用Layout布局将view包含,否则在程序中找不到View -->
<LinearLayout android:id="@+id/tab01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
androidrientation="vertical">
<ListView android:id="@+id/weather" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>

<!-- 第二个Tab 对应的布局 -->
<LinearLayout android:id="@+id/tab02"
android:layout_width="fill_parent" android:layout_height="fill_parent"
androidrientation="vertical">
<TextView android:id="@+id/weath_detail"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:text="tab">
</TextView>
</LinearLayout>

<!-- 第三个Tab 对应的布局 -->
<LinearLayout android:id="@+id/tab03"
android:layout_width="fill_parent" android:layout_height="fill_parent"
androidrientation="vertical">
<TextView android:id="@+id/city_detail" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="tab">
</TextView>
</LinearLayout>
</FrameLayout>

注意:每个Tab页面都要有自己的layout,负责在代码中通过Id无法找到相对应的视图
二、创建展示TabHost的Activity。
     1、我们可以直接继承TabActivity,再通过getTabHost()方法得到TabHost对象。例如:
     protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        myTabHost = this.getTabHost();
        //绑定布局文件
        LayoutInflater.from(this).inflate(R.layout.tab_host,
            myTabHost.getTabContentView(),
            true);
        
        initTabHost();
        setTitle(weather.getCityName());
        //设定显示内容
        setContentView(myTabHost);
    }

    2,为TabHost添加要显示的tab,一个tab就是相应的一个选项卡。例如:
     //添加tab
        myTabHost.addTab(myTabHost.newTabSpec(TAB_1)  //tab的Tag
            .setIndicator("城市天气")  //tab的标题
            .setContent(R.id.tab01)); //tab的显示内容
        myTabHost.addTab(myTabHost.newTabSpec(TAB_2)
            .setIndicator("天气详情")
            .setContent(R.id.tab02));
        myTabHost.addTab(myTabHost.newTabSpec(TAB_3)
            .setIndicator("城市介绍")
            .setContent(R.id.tab03));
    3,为Tabhost添加选项卡改变监听,在选项卡改变时做相应处理。例如:
        //添加OnTabChangedListener监听,此监听为选项卡改变监听
        myTabHost.setOnTabChangedListener(this);
      监听处理方法
       public void onTabChanged(String tabId)
    {
        if (tabId.equals(TAB_1))
        {
            //初始化标签1
            initTab1();
        }
        else if (tabId.equals(TAB_2))
        {
            //初始化标签2
            initTab2();
        }
        else if (tabId.equals(TAB_3))
        {
            //初始化标签3
            initTab3();
        }
        
    }

  4,修改Tab显示内容
    private void initTab2()
    {
        //得到tab内容的视图
        TextView weathDetailText = (TextView)findViewById(R.id.weath_detail);
        weathDetailText.setText(weather.getLiveWeather());
    }

北大青鸟 发表于 2011-8-29 09:30 | 显示全部楼层
学习了,感谢楼主
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-27 14:17 , Processed in 0.132162 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表