Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManage
来源:互联网 发布:日本进口食品批发淘宝 编辑:程序博客网 时间:2024/05/13 23:53
先来一张效果图:
下面是xml文件:
首先是第一个activity_main.xml,实现tab在下面的效果:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TabHost
- android:id="@+id/maintabhost"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <!-- 此处FrameLayout和TabWidget的位置注意一下,TabWidget在FrameLayout之下,并且FrameLayout要设置一下权重这个属性 android:layout_weight="1" 这样才能实现tab在下面的效果 -->
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1" >
- </FrameLayout>
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- </TabWidget>
- </LinearLayout>
- </TabHost>
- </RelativeLayout>
下面是主Activity:
我这里继承ActivityGroup而不是继承Activity,是因为我在写代码的时候报了如下的异常: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? ,代码里有我解决方法(也就是继承ActivityGroup,再加上一行代码就行),你也可以先继承Activity看看会不会报这个异常,如果报了的话再改成这个。
- public class WeiBoActivity extends ActivityGroup {
- private TabHost mTabHost = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.weibo_main);
- mTabHost = (TabHost) findViewById(R.id.maintabhost);
- mTabHost.setup();
- // 此处是我解决异常加的一行代码,如果继承Activity的话可将此行注释
- mTabHost.setup(this.getLocalActivityManager());
- mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("首页")
- .setContent(R.id.tab1));
- mTabHost.addTab(mTabHost.newTabSpec("t2").setIndicator("消息")
- .setContent(new Intent(this, MessageActivity.class)));
- mTabHost.addTab(mTabHost.newTabSpec("t3").setIndicator("好友")
- .setContent(R.id.tab3));
- mTabHost.addTab(mTabHost.newTabSpec("t4").setIndicator("广场")
- .setContent(R.id.tab4));
- }
- }
下面是跳转界面的weibo_message.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <!-- 此处TabHost的id自已定义 -->
- <TabHost
- android:id="@+id/msgstabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </TabWidget>
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- </LinearLayout>
下面是跳转的Activity,依然继承ActivityGroup:
- public class MessageActivity extends ActivityGroup {
- private TabHost mTabHostMsg;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.weibo_message);
- mTabHostMsg = (TabHost) this.findViewById(R.id.msgstabhost);
- mTabHostMsg.setup();
- mTabHostMsg.setup(this.getLocalActivityManager());
- mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t5").setIndicator("系统消息")
- .setContent(R.id.msgtab5));
- mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t6").setIndicator("评论")
- .setContent(R.id.msgtab6));
- mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t7").setIndicator("私信")
- .setContent(R.id.msgtab7));
- }
- }
转载自:http://blog.csdn.net/zhenglingkun/article/details/8261068?reload#comments
0 0
- Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManage
- Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManager activityGroup)解决方法
- Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManager activityGroup)解决方法
- Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManager activityGroup)解决方
- java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager acti
- did you forget to add stdafx.h problem
- Android 8.0 解决OkHttp问题:A connection to xxx was leaked. Did you forget to close a response body?
- Unable to find the socket transport 'ssl' - did you forget to enable it when you configured
- VS2005,Did you forget to add '#include "stdafx.h"' to your source?
- WTL出现GUID错误 Did you forget to pass the LIBID to CComModule::Init?
- VS2005,Did you forget to add '#include "stdafx.h"' to your source?
- WTL出现GUID错误 Did you forget to pass the LIBID to CComModule::Init?
- VS2005,Did you forget to add '#include "stdafx.h"' to your source?
- VS2005,Did you forget to add '#include "stdafx.h"' to your source?
- Did you forget to add '#include "stdafx.h"' to your source?
- file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it 的解决
- No source code is available for type javax.validation.ConstraintViolation<T>; did you forget to inh
- Could not load codec 'Lucene54'. Did you forget to add lucene-backward-codecs.jar?
- [unreal4入门系列之十一] 在UE4中编写C++代码控制角色
- 判断手机是不是有语音识别功能
- IBM服务器x系列服务器常见POST报错代码
- Android 如何将图片和文字同时分享到微信
- 图片回收
- Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManage
- 图的着色问题-回溯解决
- android layout_weight讲解
- asp.net 使用onclick与OnClientClick 跳转到其他页面
- Selector状态选择器
- Java输入输出流
- ActionBar和TabHost结合使用案例
- easyui form validate
- Android平台通用安全问题分析及策略