TabHost使用

tab.xml:java

<?xml version="1.0" encoding="utf-8"?>android

<TabHost app

    android:id="@android:id/tabhost" ide

    android:layout_width="fill_parent"佈局

android:layout_height="fill_parent" ui

xmlns:android="http://schemas.android.com/apk/res/android"this

android:background="@drawable/background">spa

<RelativeLayoutxml

android:orientation="vertical"utf-8

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TabWidget 

   android:id="@android:id/tabs"

android:layout_width="fill_parent" 

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"> 

</TabWidget>

<FrameLayout

            android:id="@android:id/tabcontent"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"/>

</RelativeLayout>

</TabHost>

用來顯示Tab的容器:MainTabActivity.java

package com.tianlei.test;


import android.app.TabActivity;

import android.content.Intent;

import android.content.res.Resources;

import android.os.Bundle;

import android.widget.TabHost;

import android.widget.TabHost.OnTabChangeListener;

import android.widget.TabHost.TabSpec;

 

@SuppressWarnings("deprecation")

public class MainTabActivity extends TabActivity {

private Intent testIntent = null;

private Intent waveIntent = null;

private Intent resultIntent = null;

private Intent moreIntent;

TabHost tabHost;

TabSpec spec;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentView(R.layout.tab);//這裏使用了上面建立的xml文件(Tab頁面的佈局)

   Resources res = getResources(); // Resource object to get Drawables

   tabHost = getTabHost();  // The activity TabHost

   initIntent();

   addSpec();

   

   tabHost.setCurrentTab(0);

}

/**

* 初始化各個tab標籤對應的intent

*/

private void initIntent(){

//初始化intent

testIntent = new Intent(this, ComTestActivity.class);

waveIntent = new Intent(this, MatlabActivity.class);

resultIntent = new Intent(this, ResultActivity.class);

moreIntent =  new Intent(this, MoreActivity.class);

}

/**

* 爲tabHost添加各個標籤項

*/

private void addSpec(){

tabHost.addTab(this.buildTagSpec("test", R.string.test,

R.drawable.test, testIntent));

tabHost.addTab(this.buildTagSpec("wave", R.string.wave,

R.drawable.wave, waveIntent));

tabHost.addTab(this.buildTagSpec("result", R.string.result,

R.drawable.result, resultIntent));

tabHost.addTab(this.buildTagSpec("message",R.string.message, 

R.drawable.message,moreIntent));

}

/**

* 自定義建立標籤項的

* @param tagName 標籤標識

* @param tagLable 標籤文字

* @param icon 標籤圖標

* @param content 標籤對應的內容

* @return

*/

private TabHost.TabSpec buildTagSpec(String tagName, int tagLable,

int icon, final Intent content) {

return  this.tabHost

.newTabSpec(tagName)

.setIndicator(getResources().getString(tagLable),

getResources().getDrawable(icon))

.setContent(content);

}

裏面 包含四個Activity。分別對應四個tab。  

相關文章
相關標籤/搜索