安卓开发中高级组件之选项卡的应用

在安卓开发的UI设计中,为了使界面更加清晰、美观,我们经常用到选项卡来达到在一个界面中实现不同界面的切换。选项卡主要是由TabHost、TabWidget和FrameLayout三个组件组成,用于实现一个多标签页的用户界面,通过它可以将一个复杂的对话框分割成若干个标签页,实现对信息的分类显示和管理。使用该组件不仅可以使界面简洁大方,还可以有效地减少窗体的个数。下面,以“慧办公”项目中“辅助办公”模块为例来说明实现选项卡的一般步骤:
(1)在布局文件中添加实现选项卡所需的TabHost、TabWidget和FrameLayout组件;

<?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="fill_parent"
       android:layout_height="fill_parent">
      <LinearLayout 
            android:orientation="vertical"
            android:background="#F7F5F4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
           <include
               android:id="@+id/QD_Head"
               android:layout_width="fill_parent"
               android:layout_height="40dp"
               layout="@layout/head_title" />

           <TabWidget
               android:id="@android:id/tabs"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content">
           </TabWidget>    
           <FrameLayout
               android:id="@android:id/tabcontent" 
               android:layout_width="fill_parent"
               android:layout_height="fill_parent">
           </FrameLayout>
      </LinearLayout>
</TabHost>

(2)编写各标签页中要显示内容所对应的XML布局文件;
(3)在Activity中获取并初始化TabHost组件;

tabhost=(TabHost)findViewById(android.R.id.tabhost);
tabhost.setup();                            //初始化TabHost组件

(4)为TabHost对象添加标签页;

LayoutInflater inflater=LayoutInflater.from(this);
        inflater.inflate(R.layout.tab1,tabhost.getTabContentView());
inflater.inflate(R.layout.tab2, tabhost.getTabContentView());
tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("已到人员").setContent(R.id.linearlayout1));         //添加第一个标签页
tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("缺勤人员").setContent(R.id.linearlayout2));         //添加第二个标签页
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值