第一种,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组件的更多相关文章

  1. TabHost两种实现方式

    第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.只要定义具体Tab内容布局就行了. <?xml version="1.0 ...

  2. TabHost两种不同的实现方式

    第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.只要定义具体Tab内容布局就行了 第二种:不用继承TabActivity,在布局文件中定 ...

  3. 并发编程 ~~~ 多进程~~~进程创建的两种方式, 进程pid, 验证进程之间的空间隔离, 进程对象join方法, 进程对象其他属性

    一 进程创建的两种方式 from multiprocessing import Process import time def task(name): print(f'{name} is runnin ...

  4. 用两种以上的 方式实现一个方法或者对象,调用时打印"你好xx",已定义的代码不能做修改,自己编译的不能出现"你好"? (Javasctript)

    先上代码 const obj = { say(){ Array.from(arguments).forEach(item=>{ console.log(`${this.str} ${item}` ...

  5. JMeter接口测试-提取动态列表最后一个值的两种方法

    前言 在用JMeter做接口测试时,我们经常会遇到,一个接口返回一个json串,在这个json串中,某个节点的值是一个列表,而且这个列表的长度是动态变化的.今天我们来学习两种提取动态列表最后一个值的两 ...

  6. Python中两种处理错误方法的比较

    我所说的处理错误的方法,其实是try:,except和raise这两种. 首先抛出一个实例, dictt={'a':1,'b':2,'c':3} try: if dictt['d']>1: #字 ...

  7. python with语句上下文管理的两种实现方法

    在编程中会经常碰到这种情况:有一个特殊的语句块,在执行这个语句块之前需要先执行一些准备动作:当语句块执行完成后,需要继续执行一些收尾动作.例如,文件读写后需要关闭,数据库读写完毕需要关闭连接,资源的加 ...

  8. 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)

    Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...

  9. mysql数据库自增id重新从1排序的两种方法

    mysql默认自增ID是从1开始了,但当我们如果有插入表或使用delete删除id之后ID就会不会从1开始了哦.   使用mysql时,通常表中会有一个自增的id字段,但当我们想将表中的数据清空重新添 ...

随机推荐

  1. Word里插入表格不带左右边框

    插入表格后选中,然后开始-----段落------选择右下角的边框设置,选择无左右边框.

  2. 【Uvalive4960】 Sensor network (苗条树,进化版)

    [题意] 给出N个点,M条边,问这N个点形成的生成树的最大权值边-最小权值边的最小值 InputThe input consists of several test cases, separated ...

  3. Android Training精要(六)如何防止Bitmap对象出现OOM

    1.使用AsyncTask異步加載bitmap圖片避免OOM: class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> ...

  4. 数据挖掘十大经典算法(5) 最大期望(EM)算法

    在统计计算中,最大期望(EM,Expectation–Maximization)算法是在概率(probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Lat ...

  5. Apache ActiveMQ消息中间件的基本使用

    Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件:由于ActiveMQ是一个纯Java程式,因此只需要操作系统支援Java虚拟机,ActiveMQ便可执行. 支持Jav ...

  6. GeoTools应用-DATA

    转自:http://blog.csdn.net/cdl2008sky/article/details/7266785 一.Geotools The Open Source Java GIS Toolk ...

  7. php--常用的时间处理函数

    天地四方曰宇,往古来今曰宙 时间是世界的重要组成部分,不论花开花落,还是云卷云舒都有它的影子. 但它源起何处?又将去向何方?没人知道答案,也不需要答案,我们需要的只是一个相对的起点来标识时间,现今世界 ...

  8. Unity优化之纹理集

    发现了一个比较好用的插件:ProDrawCallOptimizer. 它是用来合并纹理和材质的,而且用起来非常简便. 操作方法: 1.将包拖入Unity5中:ps:由于版本问题,直接双击包时导入不了 ...

  9. SWFUpload无刷新文件批量上传

    一.首先将SWFUpload所有文件加入项目中,如图

  10. 一步一步写一个简单通用的makefile(三)

    上一篇一步一步写一个简单通用的makefile(二) 里面的makefile 实现对通用的代码进行编译,这一章我将会对上一次的makefile 进行进一步的优化. 优化后的makefile: #Hel ...