TabHost详解0

一、TabHost是一个选项卡容器,通过标签页将多个Activity整合到一起。

TabHost的三要素为:TabWidget、FrameLayout、List<TabSpec>。

其主要的使用方式有两种: 1.继承TabActivity,结合对应的xml配置文件导入tab选项内容体 

 2.继承Activity,结合拥有TabHost标签的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" >

    <LinearLayout
        android:id="@+id/tab1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TabHost 1" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/view2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TabHost 2" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/view3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TabHost 3" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab4"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/view4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TabHost 4" />
    </LinearLayout>

</LinearLayout>
public class MainActivity extends TabActivity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获得TabHost对象
        TabHost tb=this.getTabHost();
        LayoutInflater.from(this).inflate(R.layout.activity_main, tb.getTabContentView(), true);
        //setIndicator方法设置选项卡的标题和对应图标  setContent设置对应的内容
        tb.addTab(tb.newTabSpec("spec1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.audio)).setContent(R.id.tab1));
        tb.addTab(tb.newTabSpec("spec2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.folder)).setContent(R.id.tab2));
        tb.addTab(tb.newTabSpec("spec3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.image)).setContent(R.id.tab3));
    }
}


第二种方式:

<?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
        android:id="@+id/tabhost"
        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" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>
其中 TabWidget、FrameLayout的名字必须是固定的。

public class MainActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabHost tb=(TabHost)findViewById(R.id.tabhost);
        tb.setup();
        
        TabHost.TabSpec spec1=tb.newTabSpec("tab1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("选项卡一");
        tb.addTab(spec1);
        
        TabHost.TabSpec spec2=tb.newTabSpec("tab2");
        spec2.setContent(R.id.tab2);
        spec2.setIndicator("选项卡二");
        tb.addTab(spec2);
        
        TabHost.TabSpec spec3=tb.newTabSpec("tab3");
        spec3.setContent(R.id.tab3);
        spec3.setIndicator("选项卡三");
        tb.addTab(spec3);
    }
}

Tabhost中的常用方法:

getTag():获取标签字符串

getCurrentTab():获取当前选项卡ID

getTabWidget():根据系统规定的id:tabs来找到Tabwidget

setOnTabChangedListener():change事件



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值