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 ...
随机推荐
- Linux上安装Redis教程
Redis的安装步骤: 步骤1.安装redis必须已经安装了gcc,如果没安装gcc 就使用命令 yum install -y gcc步骤2.下载redis包 下载地址:http://download ...
- 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)
A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about a ...
- POJ 2387 链式前向星下的SPFA
(POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)
D. Innokenty and a Football League time limit per test 2 seconds memory limit per test 256 megabytes ...
- 机器人搬重物(BFS)
机器人搬重物 时间限制: 1 Sec 内存限制: 128 MB提交: 22 解决: 10[提交][状态][讨论版] 题目描述 机 器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一 ...
- 21、Flask实战第21天:常用的Flask钩子函数
在Flask中钩子函数是使用特定的装饰器装饰的函数.为什么叫钩子函数呢?是因为钩子函数可以在正常执行的代码中,插入一段自己想要执行的代码.那么这种函数就叫做钩子函数. before_first_req ...
- centos7下ip转发的配置
1.先确认ipv4配置了转发设置 vim /etc/sysctl.conf #命令1(编辑配置文件) net.ipv4.ip_forward=1 ...
- Java小问题的解决方法系列
1)IDEA中文乱码,解决方法:http://blog.csdn.net/zht666/article/details/8953516 2)卸载OpenJdk,http://my.oschina.ne ...
- 记录一次Elasticsearch线上部署后出现:org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []的问题解决
说明:ES部署了3个节点,而一般情况只要这三个节点的IP其中一个都可以连接,Web端口使用的是9500,Client连接使用的是9600,调用程序使用了ES原生Client进行连接. 解决方法: 1. ...
- [转载]iOS6新特征:UICollectionView官方使用示例代码研究
原文地址:iOS6新特征:UICollectionView官方使用示例代码研究作者:浪友dans 注:这里是iOS6新特征汇总贴链接 iOS6新特征:参考资料和示例汇总 这个链接可以学习到UIColl ...