android 完美的tabhost 切换多activity布局
TabHost在很多应用都会使用到,有时候在TabHost添加的Tab中设置view不能满足需求,因为在view中添加如PreferenceActivity相当困难.
之前在一个应用中需要实现使用TabHost中在多个Tab切换不同的Activity.一个是日志列表(ListActivity),一个是应用设置页面( PreferenceActivity )
先上效果图

上图是日志列表页面,是ListActivity

上图是设置页面,是一般的PreferenceActivity
开发流程 :
1, 编写不同的Activity,用于在TabHost中点击不同的Tab时进行切换(以下是两个例子,因为不是该博文的主要内容所以省略)
1-1, 编写LogActivity,该类是ListActivity,用于显示日志内容
1-2, 编写SettingActivity,该类是PreferenceActivity,应用的设置页面,编写方式与PreferenceActivity一样.
2, 编写主页面(MainActivity),该类是应用的入口类,在该类定义TabHost并添加相应的Activity.
// 该类需要继承ActivityGroup
public class MainActivity extends ActivityGroup {
private TabHost mTabHost; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// 设置TabHost
initTabs();
} private void initTabs() {
mTabHost = (TabHost) findViewById(R.id.tabhost);
mTabHost.setup(this.getLocalActivityManager());
// 添加日志列表的tab,注意下面的setContent中的代码.是这个需求实现的关键
mTabHost.addTab(mTabHost.newTabSpec("tab_log")
.setIndicator("日志",getResources().getDrawable(R.drawable.ic_tab_log))
.setContent(new Intent(this, LogActivity.class))); // 添加应用设置的tab,注意下面的setContent中的代码.是这个需求实现的关键
mTabHost.addTab(mTabHost.newTabSpec("tab_setting")
.setIndicator("设置",getResources().getDrawable(R.drawable.ic_tab_settings))
.setContent(new Intent(this, SettingActivity.class)));
mTabHost.setCurrentTab(1);
}
}
3, 编写main.xml,MainActivity的layout的xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
xmlns:app="http://schemas.android.com/apk/res/rbase.app.nowscore"
android:layout_height="fill_parent"
android:layout_above="@+id/adLayout">
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<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" />
</LinearLayout>
</TabHost>
</LinearLayout>
</RelativeLayout>
补充:
当在不同的Tab中按下menu按钮,会以当前的Activity为准,即在SettingActivity时按下menu按钮后会触发SettingActivity的事件,不是主页面的事件.
android 完美的tabhost 切换多activity布局的更多相关文章
- Android横竖屏切换及其对应布局加载问题
第一,横竖屏切换连带横竖屏布局问题: 如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局. 可以通过以下两种方法来切换布局: 1)在res目录下建立layout-land ...
- Android横竖屏切换及其相应布局载入问题
第一.横竖屏切换连带载入多屏布局问题: 假设要让软件在横竖屏之间切换.因为横竖屏的高宽会发生转换,有可能会要求不同的布局. 能够通过下面两种方法来切换布局: 1)在res文件夹下建立layout-la ...
- Android 常用UI控件之TabHost(1)TabHost的两种布局方式
TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...
- android 完美退出所有Activity的demo
项目地址:https://github.com/libill/myapplication 利用android的wheel和参考android完美退出程序做出来的demo,结束掉所有打开的Activit ...
- android源码解析(十七)-->Activity布局加载流程
版权声明:本文为博主原创文章,未经博主允许不得转载. 好吧,终于要开始讲讲Activity的布局加载流程了,大家都知道在Android体系中Activity扮演了一个界面展示的角色,这也是它与andr ...
- Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程
最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...
- Android tabhost下的activity怎样获取传来的值
android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...
- Android Tab与TabHost
这就是Tab,而盛放Tab的容器就是TabHost 如何实现?? 每一个Tab还对应了一个布局,这个就有点好玩了.一个Activity,对应了多个功能布局. ①新建一个Tab项目,注意,不要生成mai ...
- (转载)解决切换Fragment时布局重新实例化
解决切换Fragment时布局重新实例化 作者 instanceof 关注 2015.12.30 17:25* 字数 628 阅读 7616评论 17喜欢 23 关于Fragment 在这简单说一下F ...
随机推荐
- go语言方法实例
方便和函数的区别: 方法能给用户定义的类型添加新的行为.方法实际上也是函数,只是在声明时,在关键字func 和方法名之间增加了一个参数. package main import ( "fmt ...
- IE添加可信任站点,启用ActiveX插件批处理
添加可信任站点IP地址为:192.168.1.108,启用ActiveX插件执行以下批处理命令: reg add "HKCU\Software\Microsoft\Windows\Curre ...
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...
- 训练指南 UVALive - 5135 (双连通分量)
layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...
- DP 贪心【p2134】百日旅行
Background 重要的不是去哪里,而是和你在一起.--小红 对小明和小红来说,2014年7月29日是一个美好的日子.这一天是他们相识100天的纪念日. (小明:小红,感谢你2场大考时默默的支持, ...
- Codechef ForbiddenSum
Mike likes to invent new functions. The latest one he has invented is called ForbiddenSum. Let's con ...
- 【后缀数组】bzoj2217 Secretary
考虑简化问题:计算一个字符串中至少出现两次的最长子串.答案一定会在sa中相邻两个后缀的lcp中.因为后缀的位置在sa中相距越远,其lcp的长度就越短,这是由于字典序的性质决定的. 于是,在s1和s2中 ...
- [CSAcademy]Connected Tree Subgraphs
题目大意: 给你一棵n个结点的树,求有多少种染色方案,使得染色过程中染过色的结点始终连成一块. 思路: 树形DP. 设f[x]表示先放x时,x的子树中的染色方案数,y为x的子结点. 则f[x]=pro ...
- 1.3(Spring学习笔记)Spring-AOP
一.AOP简介 AOP面向切面编程,是将一种横向抽取机制,将多个类中需要使用的方法提取出来. 例如,这里有两个类,一个Cat,一个Dog,动物都需要吃饭睡觉,如果按照传统的思想. 给两类中都添加吃饭和 ...
- springmvc与前端数据交互实例
一.从页面接收参数 Spring MVC接收请求提交的参数值的几种方法: 使用HttpServletRequest获取. @RequestMapping("/login.do" ...