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字段,但当我们想将表中的数据清空重新添 ...
随机推荐
- php常用函数集
网络请求: /** * 发起HTTPS请求 */ function curl_post($url,$data=null,$header=null,$post=0) { //初始化curl $ch = ...
- nutch http file 截断问题
问题: 列表页预计抽取 355+6 但实际只抽取到220条链接. 原因是nutch对http下载的内容的长度进行了限制. 解决方案:这里将这个属性扩大10倍. vim conf/nutch-defal ...
- 转:Java学习路线图
作者: nuanyangyang 标 题: Java学习路线图(整理中,欢迎纠正) 发信站: 北邮人论坛 (Mon Aug 11 19:28:16 2014), 站内 [以下肯定是不完整的列表, ...
- 两种方法,获取磁盘剩余空间--PYTHON
import ctypes import os import platform import sys def get_free_space_mb(folder): """ ...
- [cocos2d] 显示状态与文字
前言: 对于显示数值与文字一般有三种方式可以使用: CCLabelTTF .CCLabelBMFont .CCLabelAtlas 详细区别可参考:cocos2d 中添加显示文字的三种方式(CCLab ...
- 《unix网络编程》笔记
inet_pton和inetntop函数.字母p和n代表presentation和numeric.地址的表达presentation格式通常是ASCIL串,数值(numeric)格式则是存在于套接字地 ...
- java.lang.String内部结构的变化
原文:http://java-performance.info/changes-to-string-java-1-7-0_06/ 作者:Mikhail Vorontsov IMPORTANT: Jav ...
- 【CF】196 Div.2 D. Book of Evil
显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...
- 【HDOJ】2844 Coins
完全背包. #include <stdio.h> #include <string.h> ], c[]; int n, m; ]; int mymax(int a, int b ...
- git tag的使用
查看所有的标签git tag 删除某一个标签git tag -d tagName 创建带注释的标签 git tag -a tagName -m "annotate" 轻量级标签 g ...