自訂豎型TabWidget
來源:互聯網
上載者:User
這個方法叫做“以假亂真法”先:
中心思想:把內建的TabWidget隱藏,自己建立一個左側的豎型tab欄,添加tab監聽,以此實現豎型tabwidget
第一步,建立menu.xml布局檔案<TabHost android:id=
"@android:id/tabhost"> <TabWidget android:id=
"@android:id/tabs" android:visibility=
"gone" <LinearLayout ></LinearLayout> <FrameLayout android:id=
"@android:id/tabcontent"/></TabHost>從id的取名就可以大致看出TabHost、TabWidget、FrameLayout各自的作用TabWidget--用來定義選項卡,此法裡面一定要有這句話android:visibility=
"gone"FrameLayout--用來顯示選項卡對應的內容,通常是一些Activity中間的<LinearLayout>節點就有意思了,這個是“以假亂真”的關鍵。左側豎型tab就是在這個裡面定義的
第二步,建立每個Activity的xml檔案這裡所說的Activity就是要包含進右側frameLayout中的Activity
第三步,代碼實現tabWidget初始化// 得到TabHost對象
tabHost=
getTabHost(); Intent intent1 =
newIntent(
this,
MenuSetActivity.
class); // 第一個參數是用來指定標籤的; // 第二個參數是顯示到頁面上的tag,在這裡不會顯示到頁面,因為設定visibility
gone; // 第三個參數是選項卡內容 spec1=
tabHost.newTabSpec(
SETTAG).setIndicator(
SETTAG).setContent(intent1);
tabHost.addTab(spec1);
第四步,添加監聽@Override
public
voidonClick(View
v) {
intcheckedId
= v.getId();
if(currentId==
checkedId){ //當點擊的tab與當前tab一致時,返回
return; }
switch(checkedId)
{
caseR.id.
systemset_set_text:
setCurrentTabByTag(
SETTAG);
break; }setCurrentTabByTag就是根據當前的tag去尋找相應的tab