1. 程式人生 > >關於利用TabHost簡單實現底部導航欄的設計

關於利用TabHost簡單實現底部導航欄的設計

TabHost.java
package com.example.ddf;

import java.io.Serializable;

import com.example.ddf.SelfRoomActivity;
import com.example.ddf.MainActivity;
import com.example.ddf.SearchFoodActivity;
import com.example.ddf.AddFoodActivity;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class TabHostActivity extends TabActivity {

	  private TabHost tabHost;
	    private int []mTabImage=new int[]{R.drawable.shouye,R.drawable.sousuo,
	    R.drawable.tianjia,R.drawable.geren};
	    private String[]mTabTag=new String[]{"tab1","tab2","tab3","tab4"};
	    private int []mTabText=new int[]{R.string.shouye,R.string.sousuo,R.string.tianjia,R.string.geren};
	    private Class<?>[] mTabClass=new Class<?>[]{MainActivity.class,SearchFoodActivity.class,AddFoodActivity.class,SelfRoomActivity.class};
	   
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tabhost);
	      initUI();
	    
	}
	 private void initUI()
	    {
	       // this.setTitle(R.string.button2);
	        
	    	/* this.mTabHost=this.getTabHost();	   
	    	 mTabHost.setup();
	    	 LayoutInflater e=LayoutInflater.from(this);
	 		e.inflate(R.layout.activity_main, mTabHost.getTabContentView(),false);
	 		e.inflate(R.layout.activity_search_food, mTabHost.getTabContentView(),false);
	 		e.inflate(R.layout.activity_add_food, mTabHost.getTabContentView(),false);
	 		e.inflate(R.layout.activity_self_room, mTabHost.getTabContentView(),false);
	        //設定顯示的圖片和文字
	        for(int i=0;i<mTabClass.length;i++)
	        {
	           View view=LayoutInflater.from(this).inflate(R.layout.tab, null);
	       
	          ((ImageView)view.findViewById(R.id.image)).setImageResource(mTabImage[i]);
	         // ((TextView)view.findViewById(R.id.tabwidget_item_text)).setText(mTabText[i]);
	          
	           this.mTabHost.addTab(this.mTabHost.newTabSpec(mTabTag[i]).setIndicator(view).setContent(new Intent(this,mTabClass[i]))); 
	        }
	       
	        //設定預設選中項
	        this.mTabHost.setCurrentTab(0);*/
	       
        
		
		 this.tabHost=getTabHost();
		 for(int i=0;i<mTabClass.length;i++)
	        {
	           View view=LayoutInflater.from(this).inflate(R.layout.tab, null);
	       
	          ((ImageView)view.findViewById(R.id.image)).setImageResource(mTabImage[i]);
	          ((TextView)view.findViewById(R.id.text)).setText(mTabText[i]);
	          
	           this.tabHost.addTab(this.tabHost.newTabSpec(mTabTag[i]).setIndicator(view).setContent(new Intent().setClass(this,mTabClass[i]).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 
	        }
			/*tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator(im1)
					.setContent(new Intent().setClass(this, MainActivity.class)));
			tabHost.addTab(tabHost.newTabSpec("TAB2").setIndicator(im2)
					.setContent(new Intent().setClass(this, SearchFoodActivity.class)));
			tabHost.addTab(tabHost.newTabSpec("TAB3").setIndicator(im3)
					.setContent(new Intent().setClass(this, AddFoodActivity.class)));
			tabHost.addTab(tabHost.newTabSpec("TAB3").setIndicator(im4)
					.setContent(new Intent().setClass(this, SelfRoomActivity.class)));
	   */ }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tab_host, menu);
		return true;
	}

}
tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@layout/select_tab_bg">
    <!--tabWeight佈局-->
    <ImageView
        android:id="@+id/image"
        android:layout_width="30dp"
        android:layout_height="30dp" />
   
   <TextView 
       android:id="@+id/text"
       android:paddingLeft="5dp"
        android:layout_width="40dp"
        android:layout_height="24dp"
       />
</LinearLayout>
四個activity
而要切換的四個activity不需要新增任何程式碼