【Android开发】高级组件-选项卡
来源:互联网 发布:java 空格 显示问号 编辑:程序博客网 时间:2024/06/09 02:15
选项卡主要由TabHost、TabWidget和FrameLayout3个组件组成,用于实现一个多标签页的用户界面,通过它可以将一个复杂的对话框分割成若干个标签页,实现对信息的分类显示和管理。使用该组件不仅可以使界面简洁大方,还可以有效地减少窗体的个数。
在Android中,实现选项卡的一般步骤如下:
(1)在布局文件中添加实现选项卡所需的TabHost、TabWidget和FrameLayout组件。
(2)编写各标签页中需要显示内容所对应的XML布局文件。
(3)在Activity中,获取并初始化TabHost组件。
(4)为TabHost对象添加标签页
下面通过一个实例来说明选项卡的应用
在main.xml布局文件中,首先添加一个TabHost组件,然后在该组件中添加线性布局管理器,并且在该布局中添加一个作为标签组的TabWidget和一个作为标签内容的FrameLayout组件。
res/layout/main.xml:
编写各标签页中要显示内容对应的XML布局文件:
res/layout/tab1.xml:
res/layout/tab2.xml:
res/layout/tab3.xml:
MainActivity:
效果如图:
要想把选项卡标题放在底部,实现这个很简单,只需将布局文件 中<TabWidget />
标签加个android:layout_gravity="bottom",选项卡就会显示在页面底部,
默认是 android:layout_gravity="top"。或者在LinearLayout布局下将<TabWidget />
在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组件的对象@Overrideprotected 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
0 2
- 【Android开发】高级组件-选项卡
- 安卓高级组件-------选项卡
- 【Android开发】高级组件-进度条
- 【Android开发】高级组件-自动完成文本框
- 【Android开发】高级组件-图像切换器
- 【Android开发】高级组件-网格视图
- 【Android开发】高级组件-画廊视图
- Android入门开发之高级界面组件
- 基于JQ的选项卡组件开发
- android用户界面-组件Widget-选项卡Tab
- android学习--TabHost选项卡组件
- [阶段二]Android UI选项卡组件
- Android快速开发-选项卡
- Android高级组件
- Android高级组件
- 【Android开发】高级组件-拖动条和星级评分条
- Android开发-高级网络组件(1)使用Volley登陆-AndroidStudio
- 安卓开发中高级组件之选项卡的应用
- MyBatis,Spring整合简易教程
- Spring+hibernate(jpa)+freemarker之数据库连接加密
- haar-cascade training需要自己编译exe文件
- 6.1 Java8增强的包装类
- Android 5.x OTA Update官方文档(十、版本签名)
- 【Android开发】高级组件-选项卡
- 关于PIC单片机18系列超低功耗软件编程的关键点
- HTML5-桌面式web应用-地理定位, webworker, 会话历史
- 关于html音频以及视频标签video audio标签的实例
- 第五周 阅读程序 4 const 的用法
- ASP.NET js控制TextBox只能输入数字和小数点
- 怎样写好求职简历?
- 第四周项目4:指向学生类的指针
- 记一次tcmalloc分配内存引起的coredump