天天看點

Android控件 TabHost,Android控件開發之TabHost

效果圖



Android控件 TabHost,Android控件開發之TabHost

發現很多微薄如騰訊,新浪的頁籤 都是顯示在頁面底部的,網上有資料:通過反射擷取tabwidget中的私有變量,改變其值。

效果圖

Android控件 TabHost,Android控件開發之TabHost

本程式main.xml源碼

android:id="@android:id/tabhost"android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

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

android:layout_weight="1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

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

android:layout_alignParentBottom="true"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

java源碼

import android.app.TabActivity;

import android.os.Bundle;

import android.widget.TabHost;

import android.widget.Toast;

import android.widget.TabHost.OnTabChangeListener;

public class TabWidgetActivity extends TabActivity

{

TabHost tabhost;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//取得TabHost對象

tabhost=getTabHost();

//為TabHost添加标簽

//建立一個newTabSpec(newTabSpec)

//設定其标簽和圖示(setIndicator)

//設定内容(setContent)

tabhost.addTab(tabhost.newTabSpec("tab1")

.setIndicator("TAB 1",getResources().getDrawable(R.drawable.icon))

.setContent(R.id.text1));

tabhost.addTab(tabhost.newTabSpec("tab2")

.setIndicator("TAB 2",getResources().getDrawable(R.drawable.icon))

.setContent(R.id.text2));

tabhost.addTab(tabhost.newTabSpec("tab3")

.setIndicator("TAB 3",getResources().getDrawable(R.drawable.icon))

.setContent(R.id.text3));

//設定TabHost的背景顔色

//tabhost.setBackgroundColor(Color.argb(150,22,70,150));

//設定TabHost的背景圖檔資源

tabhost.setBackgroundResource(R.drawable.bg0);

//設定目前顯示哪個标簽

tabhost.setCurrentTab(0);

//标簽切換事件處理,setOnTabChangedListener

tabhost.setOnTabChangedListener(new OnTabChangeListener()

{

public void onTabChanged(String tabId)

{

switch(tabhost.getCurrentTab())

{

case 0:

tabhost.setBackgroundResource(R.drawable.bg0);

Toast.makeText(getApplicationContext(), "目前标簽為第一個頁面", Toast.LENGTH_SHORT).show();

break;

case 1:

tabhost.setBackgroundResource(R.drawable.bg1);

Toast.makeText(getApplicationContext(), "目前标簽為第二個頁面", Toast.LENGTH_SHORT).show();

break;

case 2:

tabhost.setBackgroundResource(R.drawable.bg2);

Toast.makeText(getApplicationContext(), "目前标簽為第三個頁面", Toast.LENGTH_SHORT).show();

break;

}

}

});

}

}