Android中关于TabHost的使用简介

在Android UI开发时,使用Tab选项卡可以实现多个不同标签内容的切换,要实现这个效果,首先要得到TabHost对象,它是用来存放多个Tab标签的容器,每个Tab都可以对其显示的内容进行自定义布局,在得到TabHost后,通过addTab()方法来向TabHost中添加标签,当然Tab在切换时会产生相应的事件,该事件需用OnTabChangedListener监听。

1、定义要显示的布局文件tabhost.xml,如下

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
            android:orientation="horizontal"
            android:background="@drawable/bg"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/tab_music"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/music"
                android:textColor="#f690" />

            <TextView
                android:id="@+id/tab_vedio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/vedio"
                android:textColor="#f690" />

            <TextView
                android:id="@+id/tab_read"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/read"
                android:textColor="#f690" />

            <TextView
                android:id="@+id/tab_sport"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/sport"
                android:textColor="#f690" />
        </FrameLayout>
    </LinearLayout>

</TabHost>

注意在此种布局文件中TabWidget的id需是@android:id/tabs,FrameLayout的id需是@android:id/tabcontent。


2、可以对Tab的切换做相应的事件处理,比如使用TabHost可以切换到Activity的内容 等。

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

public class TabBarActivity extends Activity implements OnTabChangeListener 
{
	private TabHost tabHost;
	
	private TabSpec spec;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tabhost);
		// 获得TabHost的对象,并进行初始化setup()
		tabHost = (TabHost) this.findViewById(R.id.tabhost);
		tabHost.setup();
		
		// 创建选项卡的标签  
		spec = tabHost.newTabSpec("music");
		// 设置该标签页的布局内容
		spec.setContent(R.id.tab_music);
		// 设置选项卡显示标题
		spec.setIndicator("音乐");
		tabHost.addTab(spec);
		
		spec = tabHost.newTabSpec("vedio");
		spec.setContent(R.id.tab_vedio);
		spec.setIndicator("影视");
		tabHost.addTab(spec);
		
		spec = tabHost.newTabSpec("read");
		spec.setContent(R.id.tab_read);
		spec.setIndicator("图书");
		tabHost.addTab(spec);
		
		spec = tabHost.newTabSpec("sport");
		spec.setContent(R.id.tab_sport);
		spec.setIndicator("体育");
		tabHost.addTab(spec);
		
		// 可通过setCurrentTab(index)指定显示的页
		tabHost.setCurrentTab(0);
		
		tabHost.setOnTabChangedListener(this);
	}

	// 相应事件的处理
	@Override
	public void onTabChanged(String tabId) 
	{
		if ("music".equals(tabId)) 
		{
			Toast.makeText(TabBarActivity.this, "你点击了" + tabId, Toast.LENGTH_LONG)
					.show();
		}
		// TODO
		
	}

}
实现的效果如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值