博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android---61---TabHost简单使用
阅读量:7215 次
发布时间:2019-06-29

本文共 2769 字,大约阅读时间需要 9 分钟。

 

与TabHost结合使用的组件:

TabWidget:代表选项卡的标签条
TabSpec:代表选项卡的一个Tab页面

TabHost不过一个简单的容器,它提供两个方法来创建、加入选项卡

newTabSpec(String tag):创建选项卡

addTab(TabHOst.TabSpec tabSpec):加入选项卡

步骤:

1.在界面布局文件里定义TabHost组件,并为该组件定义该选项卡的内容

2.Activity继承TabActivity

3.调用TAbActivity的getTabHost()方法获取TabHost对象

4.通过TabHost对象的方法来创建、加入选项卡

 

布局文件:

 

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" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 定义第一个标签页的内容 --> <LinearLayout android:id="@+id/tab01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab1" android:textSize="11pt" /> </LinearLayout> <!-- 定义第二个标签页的内容 --> <LinearLayout android:id="@+id/tab02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab2" android:textSize="11pt" /> </LinearLayout> <!-- 定义第三个标签页的内容 --> <LinearLayout android:id="@+id/tab03" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:textSize="11pt" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab3" android:textSize="11pt" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>

 

TabHost容器内部须要组合两个组件:TabWidget和FrameLayout,当中TabWidget定义选项卡的标题条

Framelayout则用于层叠组合多个选项界面

 

Activity:

 

public class MainActivity extends TabActivity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		// 获取该Activity里面的TabHost组件		TabHost tabHost = getTabHost();		// 创建第一个Tab页		TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("01") // 设置标题				.setContent(R.id.tab01); // 设置内容		// 加入第一个标签页		tabHost.addTab(tab1);		TabSpec tab2 = tabHost				.newTabSpec("tab2")				// 在标签标题上放置图标				.setIndicator("02",						getResources().getDrawable(R.drawable.ic_launcher))				.setContent(R.id.tab02);		// 加入第二个标签页		tabHost.addTab(tab2);		TabSpec tab3 = tabHost.newTabSpec("tab3").setIndicator("03")				.setContent(R.id.tab03);		// 加入第三个标签页		tabHost.addTab(tab3);	}}

 

 

 

有时候选项卡是在下边的,仅仅须要改动布局文件就可以

 

 

 

 

布局文件:

 

 

你可能感兴趣的文章
bash shell命令(2)
查看>>
html中#include file的使用方法
查看>>
eclipse: Program "g++" not found in PATH
查看>>
Python基础(11)--面向对象1
查看>>
银行家算法
查看>>
Spring 的@Scheduled注解实现定时任务运行和调度
查看>>
Oracle笔记 四、增删改、事务
查看>>
PreTranslateMessage作用和用法
查看>>
微信支付开发教程
查看>>
一款免费好用的正则表达式工具:Regex Match Tracer
查看>>
jquery.min.map 404 (Not Found)出错的原因及解决办法
查看>>
关于浮点数的json解析
查看>>
十折交叉验证10-fold cross validation, 数据集划分 训练集 验证集 测试集
查看>>
python-切片实例
查看>>
Android8.0运行时权限策略变化和适配方案
查看>>
Latex中设置字体颜色
查看>>
通过JS控制各种元素的点击事件的【时间间隔】,特别适合【发表评论】功能...
查看>>
话说TP框架里的Vendor这目录是干什么用的啊?类库扩展thinkphp3.1版本
查看>>
Android SDK与API版本的对应关系
查看>>
Elasticsearch yellow 意味着主分片可用,副本不可用
查看>>