TabHost 两种使用方法 直接让一个Activity 继承TabActivity 和 利用findViwById()方法取得TagHost组件




第一种,TabActivity 解决方案
下面建立的布局文件,它包含多个标签的显示组件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/MyLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id ="@+id/tab_edit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <EditText
android:id ="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize= "18px"
android:text = "请输入检索关键字"
/>
<Button
android:id = "@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "搜索"
/>
</LinearLayout>
<LinearLayout
android:id ="@+id/tab_clock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical" > <AnalogClock
android:id ="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout
android:id ="@+id/tab_sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <RadioGroup
android:id ="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id ="@+id/male"
android:checked = "true"
android:text = "性别 : 男"
/>
<RadioButton
android:id ="@+id/female"
android:text = "性别 : 女"
/>
</RadioGroup>
</LinearLayout> </LinearLayout>

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class TabHostDemo extends TabActivity { private TabHost myTabHost; private int[] layRes = new int[]{R.id.tab_edit,R.id.tab_clock, R.id.tab_sex}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.tab); this.myTabHost = super.getTabHost(); //取得TabHost对象 LayoutInflater.from(this).inflate(R.layout.tab, this.myTabHost.getTabContentView(), true); for(int x=0; x < this.layRes.length; x++){ TabSpec myTab = myTabHost.newTabSpec("tab" + x); myTab.setIndicator("标签 -"+x); myTab.setContent(this.layRes[x]); this.myTabHost.addTab(myTab); } }} |



第二种 表格标签(继承Activity)利用Tab.xml布局

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id ="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<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"
android:layout_alignParentTop="true"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id ="@+id/tab_edit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <EditText
android:id ="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize= "18px"
android:text = "请输入检索关键字"
/>
<Button
android:id = "@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "搜索"
/>
</LinearLayout>
<LinearLayout
android:id ="@+id/tab_clock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical" > <AnalogClock
android:id ="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout
android:id ="@+id/tab_sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <RadioGroup
android:id ="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id ="@+id/male"
android:checked = "true"
android:text = "性别 : 男"
/>
<RadioButton
android:id ="@+id/female"
android:text = "性别 : 女"
/>
</RadioGroup>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>


package cn.TabHost; import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; public class TabHostDemo extends Activity {
private TabHost myTabHost;
private int[] layRes = new int[]{R.id.tab_edit,R.id.tab_clock,
R.id.tab_sex}; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab); myTabHost = (TabHost)findViewById(R.id.tabhost);
myTabHost.setup(); //建立TabHost对象
for(int x=0; x < this.layRes.length; x++ ) {
TabSpec myTab = myTabHost.newTabSpec("tag" + x);
myTab.setIndicator("标签 - " + x);
myTab.setContent(this.layRes[x]);
myTabHost.addTab(myTab); }
this.myTabHost.setCurrentTab(2); //默认显示的标签索引为2
}
}


第三种 表格标签在下方显示
就是在Tab.xml中把 LinearLayout改成RelativeLayout。 TabWidget 标签加入 android:layout_alignParentBottom="true"

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id ="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
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"
android:layout_alignParentBottom="true"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id ="@+id/tab_edit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <EditText
android:id ="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize= "18px"
android:text = "请输入检索关键字"
/>
<Button
android:id = "@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "搜索"
/>
</LinearLayout>
<LinearLayout
android:id ="@+id/tab_clock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical" > <AnalogClock
android:id ="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout
android:id ="@+id/tab_sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <RadioGroup
android:id ="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id ="@+id/male"
android:checked = "true"
android:text = "性别 : 男"
/>
<RadioButton
android:id ="@+id/female"
android:text = "性别 : 女"
/>
</RadioGroup>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>


TabHost 两种使用方法 直接让一个Activity 继承TabActivity 和 利用findViwById()方法取得TagHost组件的更多相关文章
- TabHost两种实现方式
第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.只要定义具体Tab内容布局就行了. <?xml version="1.0 ...
- TabHost两种不同的实现方式
第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.只要定义具体Tab内容布局就行了 第二种:不用继承TabActivity,在布局文件中定 ...
- 并发编程 ~~~ 多进程~~~进程创建的两种方式, 进程pid, 验证进程之间的空间隔离, 进程对象join方法, 进程对象其他属性
一 进程创建的两种方式 from multiprocessing import Process import time def task(name): print(f'{name} is runnin ...
- 用两种以上的 方式实现一个方法或者对象,调用时打印"你好xx",已定义的代码不能做修改,自己编译的不能出现"你好"? (Javasctript)
先上代码 const obj = { say(){ Array.from(arguments).forEach(item=>{ console.log(`${this.str} ${item}` ...
- JMeter接口测试-提取动态列表最后一个值的两种方法
前言 在用JMeter做接口测试时,我们经常会遇到,一个接口返回一个json串,在这个json串中,某个节点的值是一个列表,而且这个列表的长度是动态变化的.今天我们来学习两种提取动态列表最后一个值的两 ...
- Python中两种处理错误方法的比较
我所说的处理错误的方法,其实是try:,except和raise这两种. 首先抛出一个实例, dictt={'a':1,'b':2,'c':3} try: if dictt['d']>1: #字 ...
- python with语句上下文管理的两种实现方法
在编程中会经常碰到这种情况:有一个特殊的语句块,在执行这个语句块之前需要先执行一些准备动作:当语句块执行完成后,需要继续执行一些收尾动作.例如,文件读写后需要关闭,数据库读写完毕需要关闭连接,资源的加 ...
- 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)
Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...
- mysql数据库自增id重新从1排序的两种方法
mysql默认自增ID是从1开始了,但当我们如果有插入表或使用delete删除id之后ID就会不会从1开始了哦. 使用mysql时,通常表中会有一个自增的id字段,但当我们想将表中的数据清空重新添 ...
随机推荐
- tableView被Nav挡住了
// 1. // self.navigationController.navigationBar.translucent = NO; // self.tabBarController.ta ...
- 转:浅谈命令查询职责分离(CQRS)模式
原文来自于:http://www.cnblogs.com/yangecnu/p/Introduction-CQRS.html 在常用的三层架构中,通常都是通过数据访问层来修改或者查询数据,一般修改和查 ...
- C语言程序设计做题笔记之C语言基础知识(下)
C 语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行 事.并且C是相当灵活的,用于执行计算机程序能完成的 ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 曼哈顿生成树
大致题意:统计平面上由曼哈顿距离小于等于c的点对组成联通块的个数. 曼哈顿生成树的模板题.有关讲解:http://blog.csdn.net/acm_cxlove/article/details/88 ...
- hdu 4452
今天模拟赛的一个模拟题: 每次看到这种题就感觉很繁琐: 这次静下心来写写,感觉还不错!就是很多错误,浪费了一点时间: 代码: #include<cstdio> #include<cs ...
- CAS单点登录配置[5]:测试与总结
终于要结束了... 测试 1 我们同时打开Tomcat6和Tomcat7,如果报错请修改. 打 开浏览器,输入http://fighting.com/Client1,进入CAS登录界面,这里我们先输入 ...
- 视频硬解api介绍
在一个gpu如此强大的时代,视频解码怎么能少了gpu厂商的参加.为了用硬件加速视频解码,厂商定义了一些api. 好吧,一旦和硬件打交道,就会有os的参加,有了硬件与os参加,api肯定会变成很凌乱,看 ...
- hud1520Anniversary party(树形DP)
链接 第一道树形DP 根据左儿子 右兄弟 将多叉树转化成二叉树 结构体里保存取这个节点和不取这个节点的最大值 #include <iostream> #include<cstdio& ...
- VS2010中 为图片添加背景图片
很简单的东西,嘿嘿 void CTestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage( ...
- 使用SQL Server 2014内存数据库时需要注意的地方
作者 王枫发布于2014年7月4日 本文从产品设计和架构角度分享了Microsoft内存数据库方面的使用经验,希望你在阅读本文之后能够了解这些新的对象.概念,从而更好地设计你的架构. 内存数据库,指的 ...
