本文主要参考自:http://blog.csdn.net/wulianghuan/article/details/8588947 (里面有TabHost第二种定义的方式,继承TabActivity)

TabHost就是一个选项卡,类似于tab。这里我定义了三个标签,点击后会切换出不同的内容。之前用Fragment实现过类似的效果,总觉得还是Fragment+tab好用点。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TabHost
android:id="@+id/tablehost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > </FrameLayout>
</LinearLayout>
</TabHost> </LinearLayout>

每个选项卡点击后的内容的布局。其实就是在上面定义的FrameLayout中的内容

home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#0000FF"
android:id="@+id/home"
android:orientation="vertical"> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="home"
/> </LinearLayout>

comment.xml / save.xml布局和home一样。

下面是java代码——MainActivity.java

package com.example.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //取得TabHost
TabHost tabhost = (TabHost) findViewById(R.id.tablehost);
tabhost.setup();
LayoutInflater inflater = LayoutInflater.from(this);
//把配置文件转换为显示TabHost内容的FrameLayout中的层级
inflater.inflate(R.layout.home, tabhost.getTabContentView());
inflater.inflate(R.layout.comment, tabhost.getTabContentView());
inflater.inflate(R.layout.save, tabhost.getTabContentView());
//设置HOME标签
TabSpec spec1 = tabhost.newTabSpec("HOME").
setIndicator("HOME"); // setIndicator设置标题
//设置HOME模块显示内容
spec1.setContent(R.id.home);
tabhost.addTab(spec1);
TabSpec spec2 = tabhost.newTabSpec("COMMENT").setIndicator("COMMENT");
spec2.setContent(R.id.comment);
tabhost.addTab(spec2); TabSpec spec3 = tabhost.newTabSpec("SAVE").setIndicator("SAVE");
spec3.setContent(R.id.save);
tabhost.addTab(spec3); }
}

TabHost的初步使用的更多相关文章

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

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

  2. Robotium原理初步--Android自动化测试学习历程

    章节:自动化基础篇——Robotium原理初步(第四讲) 主要讲解内容与笔记: 一.基于控件 1.spinner——下拉菜单 2.TabHost——左右滑动选择菜单,类似电话本 3.Gallery—— ...

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

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

  4. 移动端之Android开发的几种方式的初步体验

    目前越来越多的移动端混合开发方式,下面列举的大多数我都略微的尝试过,就初步的认识写个简单的心得: 开发方式 开发环境 是否需要AndroidSDK 支持跨平台 开发语言&技能 MUI Win+ ...

  5. android 使用Tabhost 发生could not create tab content because could not find view with id 错误

    使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误. 总结一下发生错误的原因,一般的 ...

  6. CSharpGL(29)初步封装Texture和Framebuffer

    +BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffe ...

  7. Android自定义View初步

    经过上一篇的介绍,大家对于自定义View一定有了一定的认识,接下来我们就以实现一个图片下显示文字的自定义View来练习一下.废话不多说,下面进入我们的正题,首先看一下我们的思路,1.我们需要通过在va ...

  8. 初步认识Node 之Node为何物

    很多人即便是在使用了Node之后也不知道它到底是什么,阅读完本文你应该会有一个初步的.具体的概念了.    Node的目标 提供一种简单的构建可伸缩网络程序的方法.那么,什么是可伸缩网络程序呢?可伸缩 ...

  9. [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二)

    [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二) Date  周六 10 一月 2015 By 钟谢伟 Category website develop ...

随机推荐

  1. .NetCore下使用Prometheus实现系统监控和警报 (二)Linux安装

    Prometheus对Windows有相关的支持  下载地址:https://prometheus.io/download/ wget https://github.com/prometheus/pr ...

  2. Caffe训练AlexNet网络模型——问题三

    caffe 进行自己的imageNet训练分类:loss一直是87.3365,accuracy一直是0 解决方法: http://blog.csdn.net/jkfdqjjy/article/deta ...

  3. spark web ui中的skipped的含义

    顾名思义,跳出的意思啦. 例如如图: skipped的stages代表是已经执行过了.所以不需要再执行了. 如何,你有一个 testRdd.然后先做 testRdd.Filter("xxx& ...

  4. Windows下使用 Sublime Text + MinGW 搭建C/C++开发环境

    下载并安装 Sublime Text 点击此处从官网下载适合自己的Windows系统的Sublime Text 下载好后双击进行安装(一路next就好啦) 下载 MinGW 点击此处下载MinGW 下 ...

  5. 在windows下使用多版本Python安装相应的虚拟开发环境

    在windows下面使用多版本Python安装相应的虚拟开发环境   在搭建一个项目的时候,希望使用最新版的Python3.7版本,但是Python3.6的版本也要留下,那么问题来了,如何解决这个问题 ...

  6. MySQL数据库引擎详解

    作为Java程序员,MySQL数据库大家平时应该都没少使用吧,对MySQL数据库的引擎应该也有所了解,这篇文章就让我详细的说说MySQL数据库的Innodb和MyIASM两种引擎以及其索引结构.也来巩 ...

  7. C#泛型的抗变与协变

    C#泛型的抗变与协变 学习自 C#本质论6.0 https://www.cnblogs.com/pugang/archive/2011/11/09/2242380.html Overview 一直以来 ...

  8. NOIP练习赛题目5

    小象涂色 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 小象喜欢为箱子涂色.小象现在有c种颜色,编号为0~c-1:还有n个箱 ...

  9. MyBatis3与Spring3无缝集成-从iBatis平滑过渡

    从2010开始接触iBatis到现在,一直到现在把iBatis作为数据访问层ORM.为了演示一个Web应用,今天又搭了个SpringMVC应用,由于应用比较简单,Spring版本直接用最新版本3.2. ...

  10. CCCC 成都信息工程大学游记

    晚上刷智障25人本,刷到深夜四点,然后迷迷糊糊8点钟起床上车睡觉,然后就到了信息工程大学. 然后开始抢衣服,抢完衣服就开始拍照. 对了,这个学校看了下地图,好小呀,不过妹子好多呀. 然后就被老师带进机 ...