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

来源:互联网 发布:日本进口食品批发淘宝 编辑:程序博客网 时间:2024/05/13 23:53

先来一张效果图:

效果图

下面是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.   
  6.     <TabHost  
  7.         android:id="@+id/maintabhost"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_centerVertical="true" >  
  12.   
  13.         <LinearLayout  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="match_parent"  
  16.             android:orientation="vertical" >  
  17.             <!-- 此处FrameLayout和TabWidget的位置注意一下,TabWidget在FrameLayout之下,并且FrameLayout要设置一下权重这个属性 android:layout_weight="1" 这样才能实现tab在下面的效果 -->  
  18.             <FrameLayout  
  19.                 android:id="@android:id/tabcontent"  
  20.                 android:layout_width="fill_parent"  
  21.                 android:layout_height="0dip"  
  22.                 android:layout_weight="1" >  
  23.             </FrameLayout>  
  24.   
  25.             <TabWidget  
  26.                 android:id="@android:id/tabs"  
  27.                 android:layout_width="fill_parent"  
  28.                 android:layout_height="wrap_content" >  
  29.             </TabWidget>  
  30.         </LinearLayout>  
  31.     </TabHost>  
  32.   
  33. </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.   
  3.     private TabHost mTabHost = null;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  9.         setContentView(R.layout.weibo_main);  
  10.                   
  11.         mTabHost = (TabHost) findViewById(R.id.maintabhost);  
  12.         mTabHost.setup();  
  13.           
  14.         // 此处是我解决异常加的一行代码,如果继承Activity的话可将此行注释  
  15.         mTabHost.setup(this.getLocalActivityManager());  
  16.           
  17.         mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("首页")  
  18.                 .setContent(R.id.tab1));  
  19.         mTabHost.addTab(mTabHost.newTabSpec("t2").setIndicator("消息")  
  20.                 .setContent(new Intent(this, MessageActivity.class)));  
  21.         mTabHost.addTab(mTabHost.newTabSpec("t3").setIndicator("好友")  
  22.                 .setContent(R.id.tab3));  
  23.         mTabHost.addTab(mTabHost.newTabSpec("t4").setIndicator("广场")  
  24.                 .setContent(R.id.tab4));  
  25.   
  26.     }  
  27.   
  28. }  

下面是跳转界面的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.   
  12.         <LinearLayout  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="match_parent"  
  15.             android:orientation="vertical" >  
  16.   
  17.             <TabWidget  
  18.                 android:id="@android:id/tabs"  
  19.                 android:layout_width="match_parent"  
  20.                 android:layout_height="wrap_content" >  
  21.             </TabWidget>  
  22.   
  23.             <FrameLayout  
  24.                 android:id="@android:id/tabcontent"  
  25.                 android:layout_width="match_parent"  
  26.                 android:layout_height="match_parent" >  
  27.             </FrameLayout>  
  28.         </LinearLayout>  
  29.     </TabHost>  
  30.   
  31. </LinearLayout>  


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

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

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

0 0
原创粉丝点击