生活随笔
收集整理的這篇文章主要介紹了
TabSpec和TabHost实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
TabSpec與TabHost
TabHost相當于瀏覽器中瀏覽器分布的集合,而Tabspec則相當于瀏覽器中的每一個分頁面。d在Android中,每一個TabSpec分布可以是一個組件,也可以是一個布局,然后將每一個分頁裝入TabHost中,TabHost即可將其中的每一個分頁一并顯示出來。
步驟:
(1)繼承TabActivity:在此之前繼承的都是android.app.Activity類,但是這里需要繼承android.app.TabActivity。
(2)創建TabHost分布菜單對象,利用以下代碼。
LayoutInflater.from(this).inflate(R.layout.main,tableHost.getTabContentView());
(3)實例化實分頁
java代碼:
[html]?view plaincopy
package?com.test;?? ?? import?android.app.Activity;?? import?android.app.TabActivity;?? import?android.os.Bundle;?? import?android.view.LayoutInflater;?? import?android.widget.TabHost;?? import?android.widget.TabHost.OnTabChangeListener;?? import?android.widget.Toast;?? import?android.widget.TabHost.TabSpec;?? ?? public?class?MainActivity?extends?TabActivity?implements?OnTabChangeListener?{?? ????/**?Called?when?the?activity?is?first?created.?*/?? ????private?TabSpec?ts1?,ts2,?ts3?;//聲明3個分頁?? ????private?TabHost?tableHost;//分頁菜單(tab的容器)?? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????tableHost?=?this.getTabHost();//實例?(分頁)菜單?? ????????//利用LayoutInflater將布局與分頁菜單一起顯示?? ????????LayoutInflater.from(this).inflate(R.layout.main,?tableHost.getTabContentView());?? ????????ts1?=?tableHost.newTabSpec("tabOne");//實例化一個分頁?? ????????ts1.setIndicator("Tab1");//設置此分頁顯示的標題?? ????????ts1.setContent(R.id.btn);//設置此分頁的資源id?? ????????ts2=tableHost.newTabSpec("tabTwo");//實例化第二個分頁?? ????????//設置此分頁顯示的標題和圖標?? ????????ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.icon));?? ????????ts2.setContent(R.id.et);?? ????????ts3=?tableHost.newTabSpec("tabThree");//實例化第三個分頁?? ????????ts3.setIndicator("Tab3");?? ????????ts3.setContent(R.id.mylayout);//設置此分頁的布局id?? ????????tableHost.addTab(ts1);//菜單中添加ts1分頁?? ????????tableHost.addTab(ts2);?? ????????tableHost.addTab(ts3);?? ????????tableHost.setOnTabChangedListener(this);????????? ????}?? ????public?void?onTabChanged(String?tabId){?? ????????if(tabId.equals("tabOne")){?? ????????????Toast.makeText(this,?"分頁1",?Toast.LENGTH_LONG).show();?? ????????}?? ????????if(tabId.equals("tabTwo")){?? ????????????Toast.makeText(this,?"分頁2",?Toast.LENGTH_LONG).show();?? ????????}?? ????????if(tabId.equals("tabThree")){?? ????????????Toast.makeText(this,?"分頁3",?Toast.LENGTH_LONG).show();?? ????????}?? ????}?? }??
布局代碼:
[html]?view plaincopy
<?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"?>?? ?? ????<Button?? ????????android:id="@+id/btn"?? ????????android:layout_width="fill_parent"?? ????????android:layout_height="fill_parent"?? ????????android:text="This?is?Tab1"?? ????????/>?? ????<EditText?? ????????android:id="@+id/et"?? ????????android:layout_width="fill_parent"?? ????????android:layout_height="wrap_content"?? ????????android:text="This?is?Tab2"?? ????????/>?? ????<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????????android:orientation="vertical"?? ????????android:layout_width="fill_parent"?? ????????android:layout_height="fill_parent"?? ????????android:id="@+id/mylayout"?? ????????android:background="@drawable/bg"?? ????????>?? ?????????<Button??? ?????????????android:layout_width="fill_parent"?? ?????????????android:layout_height="wrap_content"?? ?????????????android:text="This?is?Tab3"?? ?????????????/>?? ?????????<EditText?? ?????????????android:layout_width="fill_parent"?? ?????????????android:layout_height="wrap_content"?? ?????????????android:text="This?is?Tab3"?? ?????????????/>?? ????</LinearLayout>?? </LinearLayout>??
運行結果:
總結:監聽分頁改變事件,具體如下:
1、使用OnTabChangeListener接口,重寫OnTabChanged(String tabId)函數
2、TabHost綁定監聽器
3、判斷OnTabChanged(String tabId)中的tabId參數進行處理事件;這里的tabId對應的是實例中每個分頁傳入的分頁ID,而不是TabSpec.setIndicatior()設置的標題
總結
以上是生活随笔為你收集整理的TabSpec和TabHost实例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。