先来一张效果图:

Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManager activityGroup)解决方法_第1张图片

下面是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;@Overridepublic 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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));}}

更多相关文章

  1. Android 滑动效果进阶篇(六)—— 倒影效果
  2. 三种方式实现自定义圆形页面加载中效果的进度条
  3. Android ListView项收缩效果
  4. Android之基于XMPP Smack Openfire 单人聊天和多人聊天(发送消息
  5. Android SQLite详解及示例代码
  6. Android中打印代码的调用层次

随机推荐

  1. Android通过百度地图API用Service和Alarm
  2. Android input输入设备键值从底层到应用
  3. android直接读取项目中的sqlite数据库
  4. 【Flutter】认识目录结构
  5. android UI秘笈
  6. Android 资源,国际化,自适应
  7. 为什么我喜欢android
  8. Android 要注意的内存占用问题
  9. Android中配置和使用Google Map服务
  10. Android WindowManager与窗口管理