【Android开发】高级组件-选项卡

简介:
选项卡主要由TabHost、TabWidget和FrameLayout3个组件组成,用于实现一个多标签页的用户界面,通过它可以将一个复杂的对话框分割成若干个标签页,实现对信息的分类显示和管理。使用该组件不仅可以使界面简洁大方,还可以有效地减少窗体的个数。

在Android中,实现选项卡的一般步骤如下:
(1)在布局文件中添加实现选项卡所需的TabHost、TabWidget和FrameLayout组件。
(2)编写各标签页中需要显示内容所对应的XML布局文件。
(3)在Activity中,获取并初始化TabHost组件。
(4)为TabHost对象添加标签页

下面通过一个实例来说明选项卡的应用

在main.xml布局文件中,首先添加一个TabHost组件,然后在该组件中添加线性布局管理器,并且在该布局中添加一个作为标签组的TabWidget和一个作为标签内容的FrameLayout组件。
res/layout/main.xml:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
		android:layout_height="fill_parent">
		<TabWidget
		    android:id="@android:id/tabs"
		    android:layout_width="fill_parent"
		    android:layout_height="wrap_content"/>
		<FrameLayout
		    android:id="@android:id/tabcontent"
		    android:layout_width="fill_parent"
		    android:layout_height="fill_parent">
		</FrameLayout>
	</LinearLayout>
</TabHost>

编写各标签页中要显示内容对应的XML布局文件:
res/layout/tab1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout1"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="界面1"/>
	<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="欢迎来到界面1"/>
</LinearLayout>

res/layout/tab2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout2"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="界面2"/>
	<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="欢迎来到界面2"/>
</LinearLayout>

res/layout/tab3.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout3"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="界面3"/>
	<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="欢迎来到界面3"/>
</LinearLayout>

MainActivity:
package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;


public class MainActivity extends Activity {
	private TabHost tabHost;//声明TabHost组件的对象
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		tabHost=(TabHost)findViewById(android.R.id.tabhost);//获取tabHost对象
		tabHost.setup();//初始化TabHost组件
		
		LayoutInflater inflater=LayoutInflater.from(this);//声明并实例化一个LayoutInflater对象
		//关于LayoutInflater详细,请看我的另外一篇转载的总结
		inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
		inflater.inflate(R.layout.tab2, tabHost.getTabContentView());
		inflater.inflate(R.layout.tab3, tabHost.getTabContentView());
		tabHost.addTab(tabHost.newTabSpec("tab01")
				.setIndicator("标签页一")
				.setContent(R.id.linearLayout1));//添加第一个标签页
		tabHost.addTab(tabHost.newTabSpec("tab02")
				.setIndicator("标签页二")
				.setContent(R.id.linearLayout2));//添加第二个标签页
		tabHost.addTab(tabHost.newTabSpec("tab03")
				.setIndicator("标签页三")
				.setContent(R.id.linearLayout3));//添加第三个标签页
	}
}

效果如图:

要想把选项卡标题放在底部,实现这个很简单,只需将布局文件 中<TabWidget />
标签加个android:layout_gravity="bottom",选项卡就会显示在页面底部,
默认是 android:layout_gravity="top"。或者在LinearLayout布局下将<TabWidget />

标签将放在<FrameLayout/>标签下

转载请注明出处:http://blog.csdn.net/acmman/article/details/44904205

相关文章
|
3天前
|
Java 开发工具 Android开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的广阔天地中,Android和iOS两大平台各自占据着半壁江山。本文将深入探讨这两个平台在开发过程中的关键差异点,包括编程语言、开发工具、用户界面设计、性能优化以及市场覆盖等方面。通过对这些关键因素的比较分析,旨在为开发者提供一个清晰的指南,帮助他们根据项目需求和目标受众做出明智的平台选择。
|
3天前
|
编解码 Android开发 iOS开发
深入探索Android与iOS开发的差异与挑战
【6月更文挑战第24天】在移动应用开发的广阔舞台上,Android和iOS两大操作系统扮演着主角。它们各自拥有独特的开发环境、工具集、用户基础及市场策略。本文将深度剖析这两个平台的开发差异,并探讨开发者面临的挑战,旨在为即将踏入或已在移动开发领域奋斗的开发者提供一份实用指南。
26 13
|
2天前
|
API Android开发 开发者
`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView
【6月更文挑战第26天】`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView。它提供高效的数据视图复用,优化的布局管理,支持多种布局(如线性、网格),并解耦数据、适配器和视图。RecyclerView的灵活性、性能(如局部刷新和动画支持)和扩展性使其成为现代Android开发的首选,特别是在处理大规模数据集时。
16 2
|
2天前
|
监控 Android开发 iOS开发
探索Android与iOS开发的差异:平台、工具和用户体验的比较
【6月更文挑战第25天】在移动应用开发的广阔天地中,Android和iOS两大平台各领风骚,它们在开发环境、工具选择及用户体验设计上展现出独特的风貌。本文将深入探讨这两个操作系统在技术实现、市场定位和用户交互方面的关键差异,旨在为开发者提供一个全景式的视图,帮助他们在面对项目决策时能够更加明智地选择适合自己项目需求的平台。
|
2天前
|
缓存 测试技术 Shell
详细解读Android开发命令行完全攻略
详细解读Android开发命令行完全攻略
|
20小时前
|
安全 Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的深度对比
在移动应用开发的广阔天地中,安卓和iOS两大平台各占半壁江山。本文旨在通过数据驱动的分析方法,深入探讨这两大操作系统在开发环境、用户界面设计及市场表现等方面的差异。引用最新的行业报告和科研数据,结合技术专家的观点,本文将提供对开发者和市场分析师均有价值的洞见。
|
1天前
|
编解码 开发工具 Android开发
技术心得:打造自己的智能投屏体验——Android投屏开发入门
技术心得:打造自己的智能投屏体验——Android投屏开发入门
|
1天前
|
存储 程序员 定位技术
程序员必知:安卓的四大组件
程序员必知:安卓的四大组件
|
2天前
|
缓存 测试技术 Shell
详细解读Android开发命令行完全攻略
详细解读Android开发命令行完全攻略
|
2天前
|
Java 开发工具 Android开发
详细解读Android开发DNK开发将.c文件打包成os
详细解读Android开发DNK开发将.c文件打包成os