TabHost Tab的添加和删除

TabHost 添加Tab项:

        tabhost = this.getTabHost();  

  

        TabSpec tabSpec = tabhost.newTabSpec("news");  

        tabSpec.setIndicator("新闻");  

        tabSpec.setContent(new Intent(this, NewsActivity.class));  

        tabhost.addTab(tabSpec);  

  

        TabSpec tabSpec2 = tabhost.newTabSpec("fun");  

        tabSpec2.setIndicator("娱乐");  

        tabSpec2.setContent(new Intent(this,FunActivity.class));  

        tabhost.addTab(tabSpec2);  

  

        TabSpec tabSpec3 = tabhost.newTabSpec("sport");  

        tabSpec3.setIndicator("体育");  

        tabSpec3.setContent(new Intent(this,SportsActivity.class));  

        tabhost.addTab(tabSpec3);  

  

        TabSpec tabSpec4 = tabhost.newTabSpec("setting");  

        tabSpec4.setIndicator("设置");  

        tabSpec4.setContent(new Intent(this, SettingActivity.class));  

        tabhost.addTab(tabSpec4);  

TabHost删除Tab项

  1. mTabHost.getTabWidget().removeViewAt(mTabHost.getCurrentTab());

这样删除会有问题。

只能通过删除所有Tab项然后再依次添加。需要注意在调用clearAllTabs()方法之前,需要设置当前显示的tab,即setCurrentTab(0),否则出现空指针问题。

完整代码如下:

                tabhost.setCurrentTab(0);  

                tabhost.clearAllTabs();  

              

                TabSpec tabSpec = tabhost.newTabSpec("news");  

                tabSpec.setIndicator("新闻");  

                tabSpec.setContent(new Intent(this,  

                        NewsActivity.class));  

                tabhost.addTab(tabSpec);  

  

                TabSpec tabSpec2 = tabhost.newTabSpec("fun");  

                tabSpec2.setIndicator("娱乐");  

                tabSpec2.setContent(new Intent(this,  

                        FunActivity.class));  

                tabhost.addTab(tabSpec2);  

  

                TabSpec tabSpec3 = tabhost.newTabSpec("sport");  

                tabSpec3.setIndicator("体育");  

                tabSpec3.setContent(new Intent(this,  

                        SportActivity.class));  

                tabhost.addTab(tabSpec3);  

  

                TabSpec tabSpec4 = tabhost.newTabSpec("setting");  

                tabSpec4.setIndicator("设置");  

                tabSpec4.setContent(new Intent(EarthActivity.this,  

                        SettingActivity.class));  

                tabhost.addTab(tabSpec4);  

  

                tabhost.setCurrentTab(2);

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

先来一张效果图:

效果图

下面是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));  

    }  

  

}

 

 

你可能感兴趣的:(tabhost)