Android的TabActivity的使用

TabActivity的使用步骤:

一、在XML中布局好相应的布局,需要注意的是,布局中必须要包含有android:id="@android:id/tabhost",

  android:id="@android:id/tabs", android:id="@android:id/tabcontent"三个id。相应的布局例子如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/test1"
    tools:context=".SlideActivity" >

    <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"
            android:paddingLeft="1dip"
            android:paddingRight="1dip"
            android:paddingTop="4dip"
            android:textColor="@drawable/white" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1" >
        </FrameLayout>

      </LinearLayout>
</TabHost>

二、先要定义一个TabHost变量,然后你的Activity要继承TabActivity而不是Activity

三、然后初始化该变量,方法有两种,一种是调用系统的函数来获得,另一种是和获得其他控件一样,用ID获

  得。即mTabHost=(TabHost)findViewById(android.R.id.tabhost);

四、然后向该Tabhost变量中添加相应的Tab,相应的添加方法如下:

  Intent intent = new Intent();

  Intent.setClass(本地的类名.this,想要启动的类名.class);

  TabSpec tabspec = mTabHost.newTabSpec(“相应的位置”);

  tabspec.setIndicator(标签名,图标);

  tabspec.setContent(intent);

  mTabHost.addTab(tabspec);

  注:当不是继承TabActivity的时候,必须在添加tab的时候先调用mTabHost的setup函数:

    mTabHost.setup(this.getLocalActivityManaget());

 

posted @ 2014-05-17 16:27  zds_song  阅读(148)  评论(0编辑  收藏  举报