第一种,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. 定位 -CLGeocoder - 编码

    #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...

  2. C++ 类族的设计

     - 类族的设计]    按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积.体积并输出并且完成要求的计算任务:    (1)先建立一个Point(点)类,包含数据成员 ...

  3. SEVERE: Error listenerStart

    转载:http://blog.sina.com.cn/s/blog_43eb83b90102e2k6.html# 今天启动Tomcat启动不了,报以下错:org.apache.catalina.cor ...

  4. Discuz!X3.1 全新安装图文教程

    http://www.discuz.net/thread-3456887-1-1.html

  5. java中的单例模式与doublecheck

    转自: http://devbean.blog.51cto.com/448512/203501 在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单 ...

  6. 行为树实现AI逻辑

    http://blog.csdn.net/kenkao/article/details/6099966 http://www.aisharing.com/archives/99 http://www. ...

  7. MySQL show status详解

    http://www.sandzhang.com/blog/2010/04/07/mysql-show-status-explained-detail/ 要查看MySQL运行状态,要优化MySQL运行 ...

  8. FFT小结

    先上模板 #include<cstdio> #include<cmath> <<)*+; typedef long long ll; ll power(ll t,; ...

  9. BZOJ1935: [Shoi2007]Tree 园丁的烦恼

    1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec  Memory Limit: 357 MBSubmit: 552  Solved: 220[Submit][ ...

  10. Excel和XML文件导入

    using System;using System.Collections;using System.Collections.Generic;using System.Configuration;us ...