与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);
}
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQ3NjU1Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

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

布局文件:

<?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" > <RelativeLayout
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"
android:layout_alignParentBottom="true" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@android:id/tabs" > <!-- 定义第一个标签页的内容 --> <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>
</RelativeLayout> </TabHost>

Android---61---TabHost简单使用的更多相关文章

  1. Android底部TabHost API

    今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...

  2. Android:PopupWindow简单弹窗改进版

    Android:PopupWindow简单弹窗 继续上一节的内容,改进一下,目标是点击菜单后把菜单收缩回去并且切换内容,我使用的是PopupWindow+RadioGroup public class ...

  3. Android学习Tabhost、gallery、listview、imageswitcher

    Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...

  4. TabHost 简单用法

    package com.google.tabhost;    import android.app.TabActivity;  import android.os.Bundle;  import an ...

  5. Android项目--tabhost

    所有牵扯到自定义布局的layout中尽量用RelativeLayout 在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的.通过tabhost来做. 1.布局 a) 直接用 ...

  6. 深入浅出Tabhost+简单入门Demo

    小伙伴们在手机上逛淘宝的时候,会发现在淘宝的下面有个按钮,分别是首页.微淘.社区.购物车和我的淘宝,点击不同的按钮会跳转到不同的页面,目前小编所接手的这个项目,也需要用到类似这样的功能,小编就发挥网络 ...

  7. Android的TabHost组件-android的学习之旅(四十)

    TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于 ...

  8. Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题

    最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...

  9. 12.Android之Tabhost组件学习

    TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...

  10. android之TabHost(下)

    首先建立res/layout/tab.xml文件 编写代码如下: <?xml version="1.0" encoding="utf-8"?> &l ...

随机推荐

  1. 编程风格——UNIX 高手的 10 个习惯

    引言 当您经常使用某个系统时,往往会陷入某种固定的使用模式.有时,您没有养成以尽可能最好的方式做事的习惯.有时,您的不良习惯甚至会导致出现混乱.纠正此类缺点的最佳方法之一,就是有意识地采用抵制这些坏习 ...

  2. .NET返回上一页

    原文发布时间为:2010-05-25 -- 来源于本人的百度文章 [由搬家工具导入] if (Request.UrlReferrer != null)                {         ...

  3. [LeetCode] 4Sum hash表

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. layui 的单选框

    <div class="layui-form-item"> <label class="layui-form-label">是否通过&l ...

  5. python 复习-2

    把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件件内容,然后反序,再追加到文件的下一行中 """把一个数字的list从小到大排序,然后写入文件,然 ...

  6. ios textfield如何设置,只能输入1.0-9.9内的数字,并实现时时监测效果

    //byzqk- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range repla ...

  7. 【转】【Stackoverflow好问题】去掉烦人的“!=null"(判空语句)

    [Stackoverflow好问题]去掉烦人的“!=null"(判空语句) 问题 为了避免空指针调用,我们经常会看到这样的语句   ...if (someobject != null) { ...

  8. An Introduction to Reflection in C++

    Apr 13, 2017 Stop me if you’ve heard this one before. You are working on a messaging middleware, a g ...

  9. delphi 按位运算 not and or xor shl shr

    delphi 按位运算 not and or xor shl shr unit Unit1;   interface   uses   Windows, Messages, SysUtils, Var ...

  10. PHP amqp扩展安装

    1.安装 rabbitmq-c下载地址:https://github.com/alanxz/rabbitmq-c> mkdir build > cd build> cmake -DO ...