- 浏览: 1042227 次
- 性别:
- 来自: 长沙
文章分类
- 全部博客 (639)
- 服务器配置篇 (58)
- hibernate篇 (14)
- spring篇 (33)
- struts篇 (28)
- JS篇 (46)
- 其他技术篇 (46)
- 数据库集群配置 (6)
- JAVA基础相关 (48)
- 分布式框架HadHoop的应用 (2)
- FLEX篇 (8)
- SQLSERVER技术 (32)
- Android学习 (13)
- amchart学习笔记 (1)
- openfire+smark搭建即时通讯 (9)
- Linux学习 (18)
- Oracle数据库 (15)
- 网站优化技术 (12)
- mysql数据库 (2)
- 项目学习总结 (18)
- 工具类(JAVA) (12)
- 工具类(JS) (2)
- 设计模式 (10)
- Lucene学习 (24)
- EJB3学习 (6)
- Sphinx搜索引擎 (3)
- 工作中用到的软件小工具 (5)
- .NET (49)
- JAVA 连接SQLSERVER2008步骤 (1)
- MongoDB (19)
- Android手机开发 (3)
- Maven (6)
- vue (9)
- Shiro (4)
- mybatis (3)
- netty框架 (1)
- SpringCloud (3)
- spring-cloud (7)
- Git (1)
- dubbo (2)
- springboot (13)
- rocketmq (1)
- git学习 (2)
- kafka服务器 (2)
- linux (10)
- WEB系统辅助项目 (1)
- jenkins (2)
- docker (4)
- influxdb (3)
- python (2)
- nginx (1)
最新评论
-
jiangfuofu555:
这样数据量大,效率怎么样?
sqlserver 实现分页的前台代码 以及后台的sqlserver语句 -
w156445045:
博主请问下,如何做到实时的刷新呢,
另外我后台是Java 谢谢 ...
web 版本的汽车仪表盘,非常好看。还有各种图形 -
jackyin5918:
<transportConnector name=&qu ...
ActiveMQ的activemq.xml详细配置讲解 -
握着橄榄枝的人:
你这个不是spring1.x的吧
spring1.x使用AOP实例 -
xiaophai:
全乱套了!
openfire+spark搭建完美的及时通讯
今天讲一下,如何在TabHost中,再放TabHost。
先来看一下效果。
一层TabHost
两层Tabhost (内部TabHots在上面)
两层TabHost (内层TabHots在下面)
下面说一下代码,一共3个 java类,3个xml布局文件。
看一下主画面:
main.xml
- <?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="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/default_bg">
- <LinearLayout android:orientation="vertical"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <TabWidget android:id="@android:id/tabs"
- android:layout_alignParentBottom="true" android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <FrameLayout android:id="@android:id/tabcontent"
- android:layout_weight="1" android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </LinearLayout>
- </TabHost>
就是常规的Tabhost布局。
入口类:
DoubleTabHost.java
- package com.yfz;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TabHost;
- /**
- * 本类继承了TabActivity
- * @author Administrator
- *
- */
- public class DoubleTabHost extends TabActivity {
- /* 注意:
- * 对于TabHost、布局文件中必须包含
- * TabHost、TabWidget 、FrameLayout
- * 如果继承TabActivity,并且通过getTabHost()方法来获取TabHost
- * 那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent
- *
- * 如果继承Activity,可以通过findViewById来获取这三个组件,此时ID可自定义
- */
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TabHost mTabHost = getTabHost();
- mTabHost.addTab(mTabHost.newTabSpec("Twitter").setIndicator(
- "Twitter",
- getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(
- new Intent(this, SubTab.class)));
- mTabHost.addTab(mTabHost.newTabSpec("Facebook").setIndicator(
- "Facebook",
- getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(
- new Intent(this, NormalActivity.class)));
- mTabHost.setCurrentTab(0);
- }
- }
对于TabHost、布局文件中必须包含TabHost、TabWidget 、FrameLayout .缺一不可
如果加载该TabHost画面的类继承TabActivity,并且想通过getTabHost()方法来获取TabHost,getTabWidget()方法获取TabWidget,
那么TabHost、TabWidget 、FrameLayout 三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent
否则会报运行时异常,错误如下:
TabHost ID错误:
- ERROR/AndroidRuntime(8301): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
TabWidget ID 错误:
- ERROR/AndroidRuntime(8354): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
FrameLayout ID错误:
- ERROR/AndroidRuntime(8404): Caused by: java.lang.RuntimeException: Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'
子TabHost页面:
subtab.xml
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/mytabhost" android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/default_bg">
- <LinearLayout android:orientation="vertical"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <!-- 注意FrameLayout/TabWidget标签的位置-->
- <FrameLayout android:id="@android:id/tabcontent"
- android:layout_weight="1" android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:id="@+id/widget59"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="你在想什么?"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- >
- </TextView>
- <TextView
- android:id="@+id/widget60"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="我在想Android"
- android:layout_alignParentTop="true"
- android:layout_alignParentRight="true"
- >
- </TextView>
- </FrameLayout>
- <TabWidget android:id="@android:id/tabs"
- android:layout_alignParentBottom="true" android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </TabHost>
子TabHost页面加载类:
SubTab.java
- package com.yfz;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TabHost;
- import android.widget.TabWidget;
- import android.widget.TextView;
- /**
- *
- * @author Administrator
- *
- */
- public class SubTab extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.subtab);
- //以下三句代码,注意顺序
- TabHost mTabHost = (TabHost)findViewById(R.id.mytabhost);
- mTabHost.setup();
- TabWidget tabWidget = mTabHost.getTabWidget();
- mTabHost.addTab(mTabHost.newTabSpec("苏州").setIndicator(
- "苏州").setContent(R.id.widget59));
- mTabHost.addTab(mTabHost.newTabSpec("上海").setIndicator(
- "上海").setContent(R.id.widget60));
- mTabHost.addTab(mTabHost.newTabSpec("天津").setIndicator(
- "天津").setContent(R.id.widget60));
- mTabHost.addTab(mTabHost.newTabSpec("北京").setIndicator(
- "北京").setContent(R.id.widget60));
- mTabHost.setCurrentTab(0);
- int height =30;
- // int width =45;
- for (int i =0; i < tabWidget.getChildCount(); i++) {
- /**设置高度、宽度,由于宽度设置为fill_parent,在此对它没效果 */
- tabWidget.getChildAt(i).getLayoutParams().height = height;
- // tabWidget.getChildAt(i).getLayoutParams().width = width;
- /**设置tab中标题文字的颜色,不然默认为黑色 */
- final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
- tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
- }
- }
- }
如果加载TabHost画面的继承自Activity,可以通过findViewById来获取这三个组件,此时ID可自定义。
需要注意的是,此时必须调用setup方法来加载TabHost。
对了,不要忘了在AndroidManifest.xml定义Activity~~呵呵!
要点就这么多~ 就这样了。
注意:如果想要把tag放到顶部或者底部,只要交换FrameLayout android:id="@android:id/tabcontent"和<TabWidget android:id="@android:id/tabs"
android:layout_alignParentBottom="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" />的位置,就实现了。
发表评论
-
安卓安装 ADV 路径包含中文报错解决方案
2012-04-15 09:28 1506今天更新了Android API,更新过程中,提示错误(某个包 ... -
Android]Android Sqlite3 常用命令
2012-02-14 10:03 1095原文链接:http://www.2cto.com/kf/201 ... -
[Android]Android的常用adb命令
2012-02-14 10:02 2926第一部分: 1. ubuntu下 ... -
[Android]使用achartengine开发曲线图相关的Android应用程序
2012-02-14 09:58 2687应用程序的开发过程中,经常会遇到和曲线图打交道的情况 ... -
android acchartengine学习入门实例
2012-02-14 09:53 1395AChartEngine(简称ACE)是Google的一个开源 ... -
Android开发经验总结——ListView的使用
2012-02-09 15:32 1351Android中ListView这个组件比较常用,但对初学者来 ... -
LinearLayOut学习
2011-11-21 16:36 1536LinearLayOut学习提供了控件的水平或垂直排列的模型。 ... -
Intent学习
2011-11-21 16:31 867Intent学习 Intent是一种运行时机制,在应用程序运 ... -
java 产生TXT文件要被安卓解析,产生的文件编码必须是UTF-8 BOM的编码
2011-11-18 14:12 1924什么是BOM BOM(byte-order mark),即字节 ... -
Android-TextView学习
2011-11-08 22:41 2005TextView控件开发学习 在主页面调用TextView的 ... -
Android模拟器的使用
2011-11-08 22:36 1075安卓模拟器的使用 1.安装好ADT(在HELP-SOFTUP ... -
Android的开发环境的搭建
2009-11-26 17:35 13271.获取并安装 Eclipse 插 ...
相关推荐
Android 嵌套TabHost示例. 两层TabHost嵌套,实现的效果类似网页上的二级菜单栏。
Android fragment 内部嵌套tabhost组件 详情请见: http://blog.csdn.net/singleton1900/article/details/27087833
tabhost中嵌套一个tabhost。
安卓Android源码——嵌套TabHost示例.zip
Android 嵌套TabHost示例.zip
Android 嵌套TabHost示例.zip源码资源下载Android 嵌套TabHost示例.zip源码资源下载
TabHost 嵌套 子Tab里面也有自己的TabHost 刚做的 0积分分享
Android 嵌套TabHost示例.zip项目安卓应用源码下载Android 嵌套TabHost示例.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
Android应用源码之嵌套TabHost示例.zip
Android TabHost嵌套示例,像电脑端一样在屏幕上显示上下嵌套的TAB效果,单击后切换内容,在内容页上又重新显示二级的TAB,实现嵌套,如示例图所示,注意:对于TabHost、布局文件中必须包含: TabHost、TabWidget ...
Android应用源码之嵌套TabHost示例.zip项目安卓应用源码下载Android应用源码之嵌套TabHost示例.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
tabhost 嵌套tabhost, 很简单的demo
安卓Android源码——精典源码之嵌套TabHost示例.zip
源码参考,欢迎下载
Android TabHost ListView初学者的实例代码
安卓开发-嵌套TabHost示例.zip.zip
1、底部tabhost嵌套顶部tabhost. 2、顶部tabhost,嵌套listview。 3、顶部tabhost子tab选项仿网易新闻功能,可以滑动,每一个对应一个listvie