您的位置:首页 > 产品设计 > UI/UE

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

2016-01-17 15:36 417 查看
在安卓开发的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));         //添加第二个标签页
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ui android 界面