ITeye博客

Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManage

Java-Object · 2013-08-16 16:55 · Android技术 · 编程语言 · Markdown

先来一张效果图:

效果图

下面是xml文件:

首先是第一个activity_main.xml,实现tab在下面的效果:

[html] view plaincopy

 

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <TabHost
  6. android:id="@+id/maintabhost"
  7. android:layout_width="fill_parent"
  8. android:layout_height="match_parent"
  9. android:layout_alignParentLeft="true"
  10. android:layout_centerVertical="true" >
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:orientation="vertical" >
  15. <!-- 此处FrameLayout和TabWidget的位置注意一下,TabWidget在FrameLayout之下,并且FrameLayout要设置一下权重这个属性 android:layout_weight="1" 这样才能实现tab在下面的效果 -->
  16. <FrameLayout
  17. android:id="@android:id/tabcontent"
  18. android:layout_width="fill_parent"
  19. android:layout_height="0dip"
  20. android:layout_weight="1" >
  21. </FrameLayout>
  22. <TabWidget
  23. android:id="@android:id/tabs"
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content" >
  26. </TabWidget>
  27. </LinearLayout>
  28. </TabHost>
  29. </RelativeLayout>

下面是主Activity:

 我这里继承ActivityGroup而不是继承Activity,是因为我在写代码的时候报了如下的异常:  java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? ,代码里有我解决方法(也就是继承ActivityGroup,再加上一行代码就行),你也可以先继承Activity看看会不会报这个异常,如果报了的话再改成这个。

[java] view plaincopy

 

  1. public class WeiBoActivity extends ActivityGroup {
  2. private TabHost mTabHost = null;
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. requestWindowFeature(Window.FEATURE_NO_TITLE);
  7. setContentView(R.layout.weibo_main);
  8. mTabHost = (TabHost) findViewById(R.id.maintabhost);
  9. mTabHost.setup();
  10. // 此处是我解决异常加的一行代码,如果继承Activity的话可将此行注释
  11. mTabHost.setup(this.getLocalActivityManager());
  12. mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("首页")
  13. .setContent(R.id.tab1));
  14. mTabHost.addTab(mTabHost.newTabSpec("t2").setIndicator("消息")
  15. .setContent(new Intent(this, MessageActivity.class)));
  16. mTabHost.addTab(mTabHost.newTabSpec("t3").setIndicator("好友")
  17. .setContent(R.id.tab3));
  18. mTabHost.addTab(mTabHost.newTabSpec("t4").setIndicator("广场")
  19. .setContent(R.id.tab4));
  20. }
  21. }

下面是跳转界面的weibo_message.xml:

[html] view plaincopy

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <!-- 此处TabHost的id自已定义 -->
  7. <TabHost
  8. android:id="@+id/msgstabhost"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" >
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:orientation="vertical" >
  15. <TabWidget
  16. android:id="@android:id/tabs"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content" >
  19. </TabWidget>
  20. <FrameLayout
  21. android:id="@android:id/tabcontent"
  22. android:layout_width="match_parent"
  23. android:layout_height="match_parent" >
  24. </FrameLayout>
  25. </LinearLayout>
  26. </TabHost>
  27. </LinearLayout>

下面是跳转的Activity,依然继承ActivityGroup:

[java] view plaincopy

 

  1. public class MessageActivity extends ActivityGroup {
  2. private TabHost mTabHostMsg;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. // TODO Auto-generated method stub
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.weibo_message);
  8. mTabHostMsg = (TabHost) this.findViewById(R.id.msgstabhost);
  9. mTabHostMsg.setup();
  10. mTabHostMsg.setup(this.getLocalActivityManager());
  11. mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t5").setIndicator("系统消息")
  12. .setContent(R.id.msgtab5));
  13. mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t6").setIndicator("评论")
  14. .setContent(R.id.msgtab6));
  15. mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t7").setIndicator("私信")
  16. .setContent(R.id.msgtab7));
  17. }
  18. }

转载自:http://blog.csdn.net/zhenglingkun/article/details/8261068?reload#comments

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。