Tabhost嵌套以及Tab中多个Activity跳转的实现
做了一个demo,先看看效果图:




源码 如下:
() DoubleTabHost
package yy.android.tab; import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost; 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可自定义
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得TabHost
TabHost mTabHost = getTabHost();
//新建一个tab并设置它的,Tag,标题,图标,内容
mTabHost.addTab(mTabHost.newTabSpec("YouTube").setIndicator(
"YouTube",
getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(
new Intent(this, SubTab.class)));
mTabHost.addTab(mTabHost.newTabSpec("Chrome").setIndicator(
"Chrome",
getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(
new Intent(this, YActivityGroup.class)));
mTabHost.setCurrentTab();//设置初始选中状态为第一个tab
}
} ////////////////////////////////////////////////////////////////////////////////////// ()YTabDActivity
package yy.android.tab; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button; public class YTabDActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normal); Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(YTabDActivity.this, SecondActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = YActivityGroup.group.getLocalActivityManager()
.startActivity("SecondActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
YActivityGroup.group.setContentView(view);
}
});
}
}
////////////////////////////////////////////////////////////////////////////////////////// ()YActivityGroup
package yy.android.tab; import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window; public class YActivityGroup extends ActivityGroup{
/**
* 一个静态的ActivityGroup变量,用于管理本Group中的Activity
*/
public static ActivityGroup group; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); group = this; } @Override//按返回键时
public void onBackPressed() {
// TODO Auto-generated method stub
//super.onBackPressed();
//把后退事件交给子Activity处理
group.getLocalActivityManager()
.getCurrentActivity().onBackPressed();
} @Override //从新获得焦点时
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
//调用搞得是onResume方法 //要跳转的界面
Intent intent = new Intent(this, YTabDActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//把一个Activity转换成一个View
Window w = group.getLocalActivityManager().startActivity("YTabDActivity",intent);
View view = w.getDecorView();
//把View添加大ActivityGroup中
group.setContentView(view);
}
}
//////////////////////////////////////////////////////////////////////////////// () SubTab
package yy.android.tab; import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView; public class SubTab extends Activity { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subtab); //获得TabHost
TabHost mTabHost = (TabHost)findViewById(R.id.mytabhost);
//当时通过findViewById来获得tabhost的而不是getTabHost获得的,在添加tab之前都需要setup
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();//设置初始时,第一个tab为选中状态 int height =;
// int width =45;
//tabWidget.getChildCount()是tab个数
for (int i =; 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));
}
}
}
///////////////////////////////////////////////////////////////////////////////// ()SecondActivity
package yy.android.tab; import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.group);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yy.android.tab"
android:versionCode=""
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DoubleTabHost"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".YActivityGroup"/>
<activity android:name=".YTabDActivity"/>
<activity android:name=".SubTab"/>
<activity android:name=".SecondActivity"/>
</application>
<uses-sdk android:minSdkVersion="" /> </manifest>
()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/ch">
<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="" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
////////////////////////////////////////////////////////
()group.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是tab的第二个界面">
</TextView>
</LinearLayout>
//////////////////////////////////////////////////////
()normal.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是tab的第一个界面">
</TextView>
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转 "/>
</LinearLayout>
//////////////////////////////////////////////////////
()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/yt">
<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="" 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="苹果IOS?"
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嵌套以及Tab中多个Activity跳转的实现的更多相关文章
- vue中嵌套的iframe中控制路由的跳转及传参
在iframe引入的页面中,通过事件触发的方式进行传递参数,其中data是一个标识符的作用,在main.js中通过data进行判断,params是要传递的参数 //iframe向vue传递跳转路由的参 ...
- tabhost中activity跳转动画不显示的解决办法
[1]如果是tabhost中的activity跳到其他的activity,用这篇blog的方法即可 http://blog.sina.com.cn/s/blog_8db8914301010t31.ht ...
- 仅在TabControl中的Tab中添加右键菜单
若想实现仅在TabControl中的Tab中添加右键菜单,可在XAML中通过使用样式得到: <TabControl> <TabControl.ItemContainerStyle&g ...
- Android中突发情况Activity数据的保存和恢复
Android中突发情况Activity数据的保存和恢复 写在前面:在我们的APP使用的过程中,总有可能出现各种手滑.被压在后台.甚至突然被杀死的情况.所以对APP中一些临时数据或关键持久型数据,就需 ...
- 关于ViewPager被嵌套在ScrollView中不显示的问题
关于ViewPager被嵌套在ScrollView中不显示的问题 进入全屏 ScrollView 嵌套ViewPager,要不是业务需求这样,估计没人愿意这么干!因为这种方式,会问题多多,简单百度一下 ...
- WPF触屏Touch事件在嵌套控件中的响应问题
前几天遇到个touch事件的坑,记录下来以增强理解. 具体是 想把一个listview嵌套到另一个listview,这时候如果list view(子listview)的内容过多超过容器高度,它是不会出 ...
- android开发中关于继承activity类中方法的调用
android开发中关于继承activity类中的函数,不能在其他类中调用其方法. MainActivity.java package com.example.testmain; import and ...
- 如何将同一个APP中的不同activity在Recent(最近任务)中显示?
需求描述 在应用Application1中存在A.B两个activity,当在应用启动了A.B activity,点击Recent键,如何让A.B两个activity都显示在Recent界面(最近任务 ...
- 在一个activity中销毁指定activity
通过静态变量的方法: 1.在Aactivity中设置一个Activity静态变量 static Activity activity; 2.在onCreate中: activity=this: 3.在B ...
随机推荐
- <input> 标签
HTML5 中的新属性. 属性 值 描述 accept mime_type 规定通过文件上传来提交的文件的类型. align left right top middle bottom 不赞成使用. ...
- 鼠标移动到表格的TD上的时候显示成一个手型的样子怎么做?
在除了IE6的情况下,可以通过CSS的:hover伪类来实现: 假如你想设定的固定区域为: <div id="test"></div>,那么只需要在CSS样 ...
- 不需要软件让Windows7变身WIFI热点
很简单,就是把一台装有windows 7操作系统,并配有无线网卡的电脑变成一台无线路由器或无线AP,以便在没有路由器的环境中实现多台无线终端(比如支持wifi的手机.电脑等设备)共享无线网络.那么我们 ...
- 手动修复OneDrive的DNS污染屏蔽的方法
随着云计算的发展和微软云战略的持续推进,使用网盘进行文档存储.协同编辑与共享已成为文档操作的新流程.而Office.Office 365和OneDrive等微软产品是Windows用户的首选.但由于国 ...
- C++ Dialog Box Command IDs
#define IDOK 1 #define IDCANCEL 2 #define IDABORT 3 #define IDRETRY 4 #define IDIGNORE 5 #define IDY ...
- [python]pep8编码规范
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...
- 对REST的一些理解
昨天学习REST,发现有篇文章写的真心不错,看了一遍,并没有完全理解,将一些感觉比较重要的做个记录. 文章链接:REST简介 定义 Representational State Transfer ( ...
- Codevs 1198 国王游戏 2012年NOIP全国联赛提高组
1198 国王游戏 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 恰逢 H 国国庆,国王邀 ...
- Python中 if __name__ == '__main__': 详解
一个python文件就可以看作是一个python的模块,这个python模块(.py文件)有两种使用方式:直接运行和作为模块被其他模块调用. __name__:每一个模块都有一个内置属性__name_ ...
- 每天一条linux命令——halt
halt命令用来关闭正在运行的Linux操作系统.halt命令会先检测系统的runlevel,若runlevel为0或6,则关闭系统,否则即调用shutdown来关闭系统. 语法: halt(选项) ...