Tab与TabHost的使用

package com.chaowen;

import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;

public class MyTab extends TabActivity{
private TabHost myTabHost;
protected int myMenuSettingTag = 0 ;
protected Menu myMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {

super .onCreate(savedInstanceState);


myTabHost
= this .getTabHost(); // 从TabActivity上面获取放置Tab的TabHost
LayoutInflater.from( this ).inflate(R.layout.main, myTabHost.getTabContentView(), true );
// from(this)从这个TabActivity获取LayoutInflater
// R.layout.main存放Tab布局
// 通过TabHost获得存放Tab标签页内容的FrameLayout
// 是否将inflate拴系到根布局元素上
myTabHost.setBackgroundColor(Color.argb( 150 , 22 , 70 , 150 ));
// 设置一下TabHost的颜色



myTabHost.addTab(myTabHost.newTabSpec(
" AA " ) // 标签一
// 显示的标题为谢霆锋,如果不想显示出来,就设为空
.setIndicator( " 谢霆锋 " ,getResources().getDrawable(R.drawable.tab))
.setContent(R.id.layout1));

myTabHost.addTab(myTabHost.newTabSpec(
" BB " )
.setIndicator(
" 李俊基 " ,getResources().getDrawable(R.drawable.tab2))
.setContent(R.id.layout2));

myTabHost.addTab(myTabHost.newTabSpec(
" CC " )
.setIndicator(
" 炎亚纶 " ,getResources().getDrawable(R.drawable.tab3))
.setContent(R.id.layout3));

// 标签切换事件
myTabHost.setOnTabChangedListener( new OnTabChangeListener() {

@Override
public void onTabChanged(String tabString) {
if (tabString.equals( " AA " )){
myMenuSettingTag
= 1 ;
}

if (tabString.equals( " BB " )){
myMenuSettingTag
= 2 ;
}

if (tabString.equals( " CC " )){
myMenuSettingTag
= 3 ;
}
if (myMenu != null ) {
onCreateOptionsMenu(myMenu);
}
}
});
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
myMenu
= menu;
myMenu.clear();

switch (myMenuSettingTag) {
case 1 :
Toast.makeText(
this , " 谢霆锋 " , Toast.LENGTH_SHORT).show();
break ;
case 2 :
Toast.makeText(
this , " 李俊基 " , Toast.LENGTH_SHORT).show();
break ;
case 3 :
Toast.makeText(
this , " 炎亚纶 " , Toast.LENGTH_SHORT).show();
break ;
}
return super .onCreateOptionsMenu(menu);
}
}

布局main.xml

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

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

     <!-- 第一个Tab对应的布局 -->

   <LinearLayout

      android:id="@+id/layout1"

      android:layout_height="fill_parent"

      android:layout_width="fill_parent"

      android:orientation="vertical"

     >

     

     <ImageView

       android:id="@+id/Image01"

       android:layout_height="fill_parent"

       android:layout_width="wrap_content"

       android:src="@drawable/i1"

       />

     

     </LinearLayout>

     <!-- 第二个Tab对应的布局 -->

      <LinearLayout

      android:id="@+id/layout2"

      android:layout_height="fill_parent"

      android:layout_width="fill_parent"

      android:orientation="vertical"

     >

     

     <ImageView

       android:id="@+id/Image02"

       android:layout_height="fill_parent"

       android:layout_width="wrap_content"

       android:src="@drawable/i2"

       />

     

     </LinearLayout>

     

     <!-- 第三个Tab对应的布局 -->

      <LinearLayout

      android:id="@+id/layout3"

      android:layout_height="fill_parent"

      android:layout_width="fill_parent"

      android:orientation="vertical"

     >

     

     <ImageView

       android:id="@+id/Image03"

       android:layout_height="fill_parent"

       android:layout_width="wrap_content"

       android:src="@drawable/i3"

       />

     

     </LinearLayout>

</FrameLayout>

Tab与TabHost的使用Tab与TabHost的使用Tab与TabHost的使用

源码下载:http://u.115.com/file/bhbmel05

你可能感兴趣的:(tabhost)