TabHost两种实现方式
第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。只要定义具体Tab内容布局就行了.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="所有通话记录"></TextView> <TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已接来电"></TextView> <TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="未接来电"></TextView> </FrameLayout>
package com.example.testtabhost; import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener; public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); TabHost th = getTabHost();
//声明TabHost,然后用LayoutInflater过滤出布局来,给TabHost加上含有Tab页面的FrameLayout
//from(this)从这个TabActivity获取LayoutInflater
//R.layout.main 存放Tab布局
//通过TabHost获得存放Tab标签页内容的FrameLayout
//是否将inflate 拴系到根布局元素上
LayoutInflater.from(this).inflate(R.layout.activity_main, th.getTabContentView(), true);
//通过TabHost获得存放Tab标签页内容的FrameLayout,
//newTabSpecd的作用是获取一个新的 TabHost.TabSpec,并关联到当前 TabHost
//setIndicator的作用是指定标签和图标作为选项卡的指示符.
//setContent的作用是指定用于显示选项卡内容的视图 ID.
th.addTab(th.newTabSpec("all").setIndicator("所有通话记录", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView01));
th.addTab(th.newTabSpec("ok").setIndicator("已接来电",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView02));
th.addTab(th.newTabSpec("cancel").setIndicator("未接来电",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView03));
//setOnTabChangeListener的作业是注册一个回调函数,当任何一个选项卡的选中状态发生改变时调用.
th.setOnTabChangedListener(
new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_LONG).show();
}
}
);
}
}
第二种:不用继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是
@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。TabHost的id可以自定义.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hometabs"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost android:id="@+id/tabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TabWidget> <FrameLayout android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout> </LinearLayout>
</TabHost>
</LinearLayout>
package com.example.testtabhost2; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabWidget; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabWidget tabWidget = tabHost.getTabWidget(); tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3)); tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2")
.setContent(R.id.view2)); final int tabs = tabWidget.getChildCount();
Log.i(TAG, "***tabWidget.getChildCount() : " + tabs); final int tabWidth = 90;
final int tabHeight = 45; for (int i = 0; i < tabs; i++) {
/* final View view = tabWidget.getChildAt(i);
view.getLayoutParams().width = tabWidth;
view.getLayoutParams().height = tabHeight;
final TextView tv = (TextView) view.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));
MarginLayoutParams tvMLP = (MarginLayoutParams)tv.getLayoutParams();
tvMLP.bottomMargin = 8;*/
}
} }
TabHost两种实现方式的更多相关文章
- Android 常用UI控件之TabHost(1)TabHost的两种布局方式
TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...
- Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- JavaScript 函数的两种声明方式
1.函数声明的方式 JavaScript声明函数有两种选择:函数声明法,表达式定义法. 函数声明法 function sum (num1 ,num2){ return num1+num2 } 表达式定 ...
- Redis两种持久化方式(RDB&AOF)
爬虫和转载请注明原文地址;博客园蜗牛:http://www.cnblogs.com/tdws/p/5754706.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...
- struts2+spring的两种整合方式
也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...
- easyui datagride 两种查询方式
easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...
- 【Visual Lisp】两种出错处理方式
两种出错处理方式:一种是对出错函数进行重定义,一种是对错误进行捕捉处理. ;;============================================================= ...
随机推荐
- Machine-learning of Andrew Ng
Machine-learning of Andrew Ng 1.基础概念 机器学习是一门研究在非特定编程条件下让计算机采取行动的学科.最近二十年,机器学习为我们带来了自动驾驶汽车.实用的语音识别.高效 ...
- orabbix自定义监控oracle
前提:安装orabbix 好后能正常运行, 检验条件(1). 最新数据有数据 (2).图形有显示 (3).日志不报错 /opt/orabbix/logs/orabbix.log 添加方法: 1. ...
- ftp 下载时防止从缓存中获取文件
//http://baike.baidu.com/link?url=QucJiA_Fg_-rJI9D4G4Z4687HG4CfhtmBUd5TlXrcWCeIEXCZxIh0TD7ng1wROAzAu ...
- 利用动态图层实现数据的实时显示(arcEngine IDynamiclayer)
marine 原文利用动态图层实现数据的实时显示(arcEngine IDynamiclayer) 说明:最近一个项目用到这方面知识,文章主要来至网络,后期会加入自己的开发心得.(以下的代码实例中,地 ...
- ZOJ 1494 Climbing Worm 数学水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494 题目大意: 一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要 ...
- POJ 1163 The Triangle 简单DP
看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...
- 【BZOJ 1096】[ZJOI2007]仓库建设
[链接] 链接 [题意] 在这里输入题意 [题解] 设f[i]表示在第i个地方设立一个仓库,且前面符合要求的最小花费. 则 \(f[i] = min(f[j] + c[i] + dis[i]*(sum ...
- embed-it_Integrator memory compile工具使用之二
embed-it_Integrator memory compile工具使用之二 主要内容 使用ish接口自动加载memory的cfg文件运行生成memory 脚本内容 打开Integrate &am ...
- 【48.51%】【poj 1611】The Suspects
Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 34447 Accepted: 16711 Description Severe ...
- Java并发包探秘 (一) ConcurrentLinkedQueue
本文是Java并发包探秘的第一篇,旨在介绍一下Java并发容器中用的一些思路和技巧,帮助大家更好的理解Java并发容器,让我们更好的使用并发容器打造更高效的程序.本人能力有限,错误难免.希望及时指出. ...