Android Tabhost使用(展示不同的Tab页)

当应用程序具有几个不同的功能页时,可以通过使用Tabhost来进行自然地分页展示,与用户友好的交互。


Tabhost的使用包含以下几点内容:

*1.在使用Tabhost控件的Activity(注意要继承自TabActivity)中setContentView(R.layout.main);

其中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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height=""
            android:background=""
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" 
            android:layout_above="@android:id/tabs"
            android:layout_height="fill_parent" />
    </RelativeLayout>
</TabHost>


其中上述几个控件的id不能改动,否则出错。但TabWidget与FrameLayout的位置可以变化,其相对位置取决于开发者的设计,其中TabWidget是代表不同tab页的那个按钮,FrameLayout加载每个不同Tab页的具体Activity。


*2.向TabHost中添加Tab页

Tabhost tabHost = getTabHost();

TabSpec tabSpec = tabHost.newTabSpec(xxxx);    //Tab页的具体描述,可以设定自定义的View来描述Tab按钮

tabHost.addTab(tabSpec);


*3. 处理不同tab页切换消息

前述的Activity应实现OnTabChangeListener,覆盖掉OnTabChangeListener的OnTabChanged方法,从而处理当不同tab页切换时的处理动作。


至此,tab页结构已经具有了基本的功能。一下为非必需选项

4. 处理不同Tab页的上下文菜单

覆盖onCreateOptionsMenu(Menu menu),根据不同Tab页Tag的不同,加载不同的菜单。

if(mTabHost.getCurrentTabTag().equals(xxxx"))
{
        this.getMenuInflater().inflate(R.menu.xxxx_menu, menu);
}

覆盖

public boolean onOptionsItemSelected(MenuItem item) {
return this.getCurrentActivity().onOptionsItemSelected(item);
}

让每个Tab页的Activity自己处理菜单选中。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值