TabHost的初步使用
本文主要参考自: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的初步使用的更多相关文章
- Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...
- Robotium原理初步--Android自动化测试学习历程
章节:自动化基础篇——Robotium原理初步(第四讲) 主要讲解内容与笔记: 一.基于控件 1.spinner——下拉菜单 2.TabHost——左右滑动选择菜单,类似电话本 3.Gallery—— ...
- Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...
- 移动端之Android开发的几种方式的初步体验
目前越来越多的移动端混合开发方式,下面列举的大多数我都略微的尝试过,就初步的认识写个简单的心得: 开发方式 开发环境 是否需要AndroidSDK 支持跨平台 开发语言&技能 MUI Win+ ...
- 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 错误. 总结一下发生错误的原因,一般的 ...
- CSharpGL(29)初步封装Texture和Framebuffer
+BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffe ...
- Android自定义View初步
经过上一篇的介绍,大家对于自定义View一定有了一定的认识,接下来我们就以实现一个图片下显示文字的自定义View来练习一下.废话不多说,下面进入我们的正题,首先看一下我们的思路,1.我们需要通过在va ...
- 初步认识Node 之Node为何物
很多人即便是在使用了Node之后也不知道它到底是什么,阅读完本文你应该会有一个初步的.具体的概念了. Node的目标 提供一种简单的构建可伸缩网络程序的方法.那么,什么是可伸缩网络程序呢?可伸缩 ...
- [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二)
[入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二) Date 周六 10 一月 2015 By 钟谢伟 Category website develop ...
随机推荐
- .NetCore 扩展封装 Expression<Func<T, bool>> 查询条件遇到的问题
前面的文章封装了查询条件 自己去组装条件,但是对 And Or 这种组合支持很差,但是也不是不能支持,只是要写更多的代码看起来很臃肿 根据 Where(Expression<Func< ...
- CSS------如何让大小不一样的div中心对齐
如图: 代码: <div style = 'display:inline;color:green;font-size:30px;font-weight:bold'>¥1666.00< ...
- 7-11Zombie's Treasure Chest uva12325
题意 你有一个体积为N的箱子和两种数量无限的宝物 宝物1的体积为s1 价值为v1 宝物2同理 输入均为32位带符号整数 你的任务是计算最多能带走多少价值的宝物 暴力枚举: 首先明白一点 ...
- 洛谷P4107 [HEOI2015]兔子与樱花 [贪心,DFS]
题目传送门 兔子与樱花 题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1 ...
- linux学习笔记-10.解压与压缩
1.gzip压缩 gzip a.txt 2.解压 gunzip a.txt.gzgzip -d a.txt.gz 3.bzip2压缩 bzip2 a 4.解压 bunzip2 a.bz2bzip2 - ...
- poj-1459-最大流dinic+链式前向星-isap+bfs+stack
title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...
- BZOJ.4355.Play with sequence(线段树)
题目链接 问题在于操作二.操作二可以拆分成:区间加\(C\).区间(对\(0\))取\(\max\). 注意到操作一的\(C\)都是非负数,即数列中不会出现负数,所以我们直接维护最小值和最小值出现的次 ...
- oracle统计字符串包含字符个数
函数:REGEXP_COUNT(); select REGEXP_COUNT('1,2,6,8,7,9',',') from dual 结果:5
- Slickflow.NET 开源工作流引擎基础介绍(十) -- 邮件轮询异步发送模块集成
前言:在任务数据生成时,为了让办理任务的用户及时获取到待办任务的主题和内容,需要发送通知类的消息,而电子邮件和手机端的短信通知则是比较普通的消息发送.本文是针对电子邮件异步发送模块的实现来做实例说明. ...
- Oracle数据库日期范围查询的两种实现方式
参考文档:http://database.51cto.com/art/201108/288058.htm Oracle数据库日期范围查询有两种方式:to_char方式和to_date方式,接下来我们通 ...