?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
TabWidget
版本:Android 3.0 r1
?
結(jié)構(gòu)
繼承關(guān)系
public class TabWidget extends LinearLayout
implements View.OnFocusChangeListener
????????
java.lang.Object
???????? android.view.View
?????????????????? android.view.ViewGroup
??????????????????????????? android.widget.LinearLayout
???????????????????????????????????? android.widget.TabWidget
?
類概述
顯示選項(xiàng)卡標(biāo)簽的列表,用于代表父選項(xiàng)卡集合的每一頁(yè)。當(dāng)前widget的容器對(duì)象是TabHost。當(dāng)用戶選擇一個(gè)選項(xiàng)卡時(shí),此對(duì)象給父容器對(duì)象TabHost發(fā)送一個(gè)消息,告訴TabHost切換到對(duì)應(yīng)的頁(yè)面顯示。此對(duì)象上的很多方法,你通常不會(huì)直接使用。容器TabHost被用來添加標(biāo)簽,添加并管理回調(diào)函數(shù)。你可能會(huì)調(diào)用此對(duì)象以迭代選項(xiàng)卡列表,或者調(diào)整選項(xiàng)卡列表的布局,但大多數(shù)方法應(yīng)該是通過容器TabHost對(duì)象調(diào)用。
參見Tab Layout tutorial。
?
XML屬性
屬性名稱 |
描述 |
android:divider |
可繪制對(duì)象,被繪制在選項(xiàng)卡窗口間充當(dāng)分割物。 |
android:tabStripEnabled |
確定是否在選項(xiàng)卡繪制 |
android:tabStripLeft |
被用來繪制選項(xiàng)卡下面的分割線左邊部分的可視化對(duì)象。 |
android:tabStripRight |
被用來繪制選項(xiàng)卡下面的分割線右邊部分的可視化對(duì)象。 |
?
公共方法
public void addView (View child)
增加子視圖。如果子視圖沒有設(shè)置布局參數(shù),那么將為子視圖設(shè)置此ViewGroup默認(rèn)的布局參數(shù)。
參數(shù)
child????????? 新增的子視圖
?
public void childDrawableStateChanged (View child)
如果調(diào)用addStatesFromChildren函數(shù)返回true,刷新此組的可視化對(duì)象的狀態(tài)(包括它的子元素的狀態(tài))。
參數(shù)
child ???????? 可視化對(duì)象狀態(tài)被改變的子元素。
?
public void dispatchDraw (Canvas canvas)
繪制時(shí)調(diào)用此方法來繪制子視圖。派生類在被繪制之前,為獲取控制可能會(huì)重寫此方法(但必須在它自己的視圖已經(jīng)被繪制之后)。
參數(shù)
canvas????? 繪制視圖的畫布(canvas)
?
public void focusCurrentTab (int index)
設(shè)置當(dāng)前選項(xiàng)卡并且讓其獲得焦點(diǎn)。此方法確保獲取焦點(diǎn)的選項(xiàng)卡窗口匹配選中的窗口,一般情況下類似 setCurrentTab(int)。通常當(dāng)我們通過操作UI實(shí)現(xiàn)時(shí),這些都不是問題,
因?yàn)?/span>UI負(fù)責(zé)調(diào)用TabWidget.onFocusChanged(),但如果我們通過程序控制選中選項(xiàng)卡窗口,那么我們就必須確認(rèn)保持焦點(diǎn)。(譯者注:setCurrentTab(int)執(zhí)行后,選中指定索引的選項(xiàng)卡窗口,但不獲取焦點(diǎn)。focusCurrentTab(int)執(zhí)行后,不僅通過調(diào)用setCurrentTab(int)
選中指定索引的選項(xiàng)卡窗口還獲取焦點(diǎn)。)
參數(shù)
index???????? 你想要設(shè)置焦點(diǎn)(橘色高亮)并且選中(選項(xiàng)卡被至于部件widget的前端)的選項(xiàng)卡。
參見
setCurrentTab(int)
?
public View getChildTabViewAt (int index)
返回位于指定索引位置的選項(xiàng)卡標(biāo)識(shí)符的視圖
參數(shù)
index???????? 選項(xiàng)卡指示符視圖返回的從零開始的索引
返回值
在提供的索引位置上的選項(xiàng)卡指示符視圖
?
public int getTabCount ()
返回選項(xiàng)卡的數(shù)量
返回值
選項(xiàng)卡指示符視圖的數(shù)量
?
public boolean isStripEnabled ()
指明選項(xiàng)卡指示符的底部分隔線是否繪制
?
public void onFocusChange (View v, boolean hasFocus)
當(dāng)視圖的焦點(diǎn)狀態(tài)被改變時(shí)調(diào)用。
參數(shù)
v???????????????? 狀態(tài)變化的視圖
hasFocus 視圖的新焦點(diǎn)狀態(tài)。
?
???????? public void sendAccessibilityEventUnchecked (AccessibilityEvent event)
??? 處理用于發(fā)送AccessibilityEvent
的請(qǐng)求。
如果再發(fā)送之前調(diào)度事件,本
方法不保證可訪問性檢查??梢酝ㄟ^調(diào)用者調(diào)用isEnabled()
檢查。
?????????????????? 參數(shù)
??????????????????????????? event??????? 事件
?
public void setCurrentTab (int index)
設(shè)置當(dāng)前選項(xiàng)卡。此方法被用來將選項(xiàng)卡置于widget的前部,并且通知其它的UI元素,一個(gè)不同的選項(xiàng)卡被調(diào)整到了前臺(tái)。注意,這不同于傳統(tǒng)的“焦點(diǎn)”,是從視圖邏輯的焦點(diǎn)。
例如,如果我們?cè)谶x項(xiàng)卡式視圖中有一個(gè)列表,一個(gè)用戶可能會(huì)上下移動(dòng)列表,通過列表選項(xiàng)移動(dòng)UI焦點(diǎn)(橘色高亮)。光標(biāo)的移動(dòng)并不影響選項(xiàng)卡的選中狀態(tài),因?yàn)闈L動(dòng)
是在同一個(gè)選項(xiàng)卡上的。選中的選項(xiàng)卡僅僅當(dāng)我們?cè)谶x項(xiàng)卡上切換時(shí)(在此例中,從列表視圖移到下一選項(xiàng)卡視圖)更改。如果想要一次性的設(shè)置焦點(diǎn)并選中選項(xiàng)卡,請(qǐng)使用setCurrentTab(int)
。
正常情況下,視圖邏輯關(guān)注調(diào)整焦點(diǎn),除非你規(guī)避了UI,你可能僅僅將你的興趣集中在這里。
參數(shù)
index????????????????? 你想要指做選中選項(xiàng)卡的選項(xiàng)卡索引(選項(xiàng)卡被顯示到widget的前部)
參見
focusCurrentTab(int)
?
public void setDividerDrawable (Drawable drawable)
設(shè)置用于在選項(xiàng)窗口指示符間分隔的可繪制對(duì)象
參數(shù)
drawable????????? 用于分隔的可繪制對(duì)象
?
public void setDividerDrawable (int resId)
設(shè)置用于在兩個(gè)選項(xiàng)卡標(biāo)識(shí)符之間進(jìn)行分隔的可繪制對(duì)象
參數(shù)
resId???????? ???????? 被用來分隔的可繪制對(duì)象的資源標(biāo)識(shí)
?
public void setEnabled (boolean enabled)
設(shè)置當(dāng)前視圖的啟用狀態(tài)。子類的不同而其對(duì)應(yīng)的啟用狀態(tài)的解釋也不相同。
參數(shù)
enabled??? 是否啟動(dòng)
?
public void setLeftStripDrawable (int resId)
設(shè)置被用來當(dāng)作選項(xiàng)卡指示符下面的分割線,左邊部分的可繪制對(duì)象。
參數(shù)
resId???????? the?? 被用來當(dāng)作左邊分隔線的可繪制對(duì)象的資源標(biāo)識(shí)符
?
public void setLeftStripDrawable (Drawable drawable)
設(shè)置被用來當(dāng)作選項(xiàng)卡指示符下面的分割線,左邊部分的可繪制對(duì)象。
參數(shù)
drawable 左邊分隔線的可繪制對(duì)象
?
public void setRightStripDrawable (int resId)
設(shè)置被用來當(dāng)作選項(xiàng)卡指示符下面的分割線,右邊部分的可繪制對(duì)象。
參數(shù)
resId???????? ???????? 被用來當(dāng)作右邊分隔線的可繪制對(duì)象的資源標(biāo)識(shí)符
?
public void setRightStripDrawable (Drawable drawable)
設(shè)置被用來當(dāng)作選項(xiàng)卡指示符下面的分割線,右邊部分的可繪制對(duì)象。
參數(shù)
drawable 右邊分隔線的可繪制對(duì)象
?
public void setStripEnabled (boolean stripEnabled)
控制選項(xiàng)卡指示符底部的分隔線是否繪制。默認(rèn)是繪制。如果用戶為選項(xiàng)卡指示符指定一個(gè)定制的視圖,那么TabHost類調(diào)用此方法以禁用繪制底部分割線。
參數(shù)
stripEnabled??? 如果需要繪制底部分割線為true
?
受保護(hù)方法
protected int getChildDrawingOrder (int childCount, int i)
返回要繪制的子元素在當(dāng)前迭代中的索引。如果你需要修改子元素的繪制順序,可以覆寫此方法。默認(rèn)情況下返回參數(shù)i值。
注意:為使此方法被調(diào)用,你必須先通過調(diào)用setChildrenDrawingOrderEnabled(boolean)
啟用子元素排序
參數(shù)
i??????? ???????? 當(dāng)前的迭代值
返回值
將要繪制的子元素在當(dāng)前迭代的索引
?
protected void onSizeChanged (int w, int h, int oldw, int oldh)
布局過程中,當(dāng)此視圖的大小發(fā)生改變時(shí)被調(diào)用的。如果你只是添加到視圖的層次結(jié)構(gòu),你調(diào)用時(shí)可以使用0當(dāng)作之前的值(old value)。
參數(shù)
w????? ???????? 此視圖的當(dāng)前寬度
h?????? ???????? 此視圖的當(dāng)前高度
oldw????????? Old此視圖之前的寬度
oldh ???????? 此視圖之前的高度
?
補(bǔ)充
???????? 術(shù)語(yǔ)對(duì)照
indicator?????????? 指示符
drawable????????? 可繪制對(duì)象
iteration??????????? 迭代
strips???????????????? 分割線
文章鏈接
Android選項(xiàng)卡(TabWidget)例子
{Android學(xué)習(xí)指南}TabWidget
Android將TAB選項(xiàng)卡放在屏幕底部
示例代碼
???????? Java:
public class TabDemo1 extends Activity implements OnClickListener {
?
???????? TabHost mTabHost = null;
???????? TabWidget mTabWidget = null;
???????? Button btnVisiable, btnStrip;
?
???????? @Override
???????? protected void onCreate(Bundle savedInstanceState) {
?????????????????? // TODO Auto-generated method stub
?????????????????? super.onCreate(savedInstanceState);
?????????????????? setContentView(R.layout.tabdemo1);
?????????????????? mTabHost = (TabHost) findViewById(android.R.id.tabhost);
?????????????????? mTabHost.setup();
???????? ???????? mTabWidget = mTabHost.getTabWidget();
?????????????????? mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(
???????????????????????????????????? R.id.LinearLayout001).setIndicator("Tab1"));
?????????????????? mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(
???????????????????????????????????? R.id.LinearLayout002).setIndicator("Tab2"));
?????????????????? mTabHost.addTab(mTabHost.newTabSpec("tab3").setContent(
???????????????????????????????????? R.id.LinearLayout003).setIndicator("Tab3"));
?
?????????????????? // mTabHost.setCurrentTab(1);
?????????????????? btnVisiable = (Button) findViewById(R.id.btnVisiable);
?????????????????? btnStrip = (Button) findViewById(R.id.btnStrip);
?????????????????? btnVisiable.setOnClickListener(this);
?????????????????? btnStrip.setOnClickListener(this);
?????????????????? setTitle("共有" + mTabWidget.getTabCount() + "個(gè)tab");
???????? }
?
???????? @Override
???????? public void onClick(View v) {
?????????????????? // TODO Auto-generated method stub
?????????????????? switch (v.getId()) {
?????????????????? case R.id.btnVisiable:
??????????????????????????? if (mTabWidget.getVisibility() != android.view.View.VISIBLE)
???????????????????????????????????? mTabWidget.setVisibility(android.view.View.VISIBLE);
??????????????????????????? else
???????????????????????????????????? mTabWidget.setVisibility(android.view.View.INVISIBLE);
??????????????????????????? break;
?
?????????????????? case R.id.btnStrip:
??????????????????????????? mTabWidget.setStripEnabled(!mTabWidget.isStripEnabled());
??????????????????????????? mTabWidget.setRightStripDrawable(android.R.color.transparent);
??????????????????????????? break;
?????????????????? default:
??????????????????????????? break;
?????????????????? }
???????? }
}???????
???????? XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
???????? android:layout_width="fill_parent" android:layout_height="fill_parent"
???????? android:id="@android:id/tabhost"
>?
???????? ?
???????? <LinearLayout android:layout_width="fill_parent"
?????????????????? android:orientation="vertical" android:layout_height="fill_parent"
???????? >
?????????????????? ??
?????????????????? <TabWidget android:id="@android:id/tabs"
??????????????????????????? android:layout_width="fill_parent" android:layout_height="wrap_content"
?????????????????? ></TabWidget>
?????????????????? <FrameLayout android:id="@android:id/tabcontent"
??????????????????????????? android:layout_height="wrap_content" android:layout_width="fill_parent"
?????????????????? >
??????????????????????????? <LinearLayout android:id="@+id/LinearLayout001" android:orientation="vertical"
???????????????????????????????????? android:layout_width="fill_parent" android:layout_height="fill_parent"
??????????????????????????? >
??????????????????????????? <Button android:id="@+id/btnVisiable" android:text="顯示狀態(tài)切換"
?????????????????????????????????????????????? android:layout_width="fill_parent" android:layout_height="wrap_content"
???????????????????????????????????? ></Button>
???????????????????????????????????? <Button android:id="@+id/btnStrip" android:layout_width="fill_parent"
?????????????????????????????????????????????? android:layout_height="wrap_content" android:text="分隔線狀態(tài)切換"
???????????????????????????????????? ></Button>
???????????????????????????????????? <TextView android:id="@+id/TextView1" android:layout_width="fill_parent"
?????????????????????????????????????????????? android:layout_height="wrap_content" android:text="小龍是好人!!!"
???????????????????????????????????? ></TextView>
????????????????????????????????????
??????????????????????????? </LinearLayout>
??????????????????????????? <LinearLayout android:id="@+id/LinearLayout002"
???????????????????????????????????? android:layout_width="fill_parent" android:layout_height="fill_parent"
??????????????????????????? >
???????????????????????????????????? <TextView android:id="@+id/TextView2" android:layout_width="fill_parent"
?????????????????????????????????????????????? android:layout_height="wrap_content" android:text="小龍是壞蛋!"
???????????????????????????????????? ></TextView>
??????????????????????????? </LinearLayout>
??????????????????????????? <LinearLayout android:id="@+id/LinearLayout003"
???????????????????????????????????? android:layout_width="fill_parent" android:layout_height="fill_parent"
??????????????????????????? >
???????????????????????????????????? <TextView android:id="@+id/TextView3" android:layout_width="fill_parent"
?????????????????????????????????????????????? android:layout_height="wrap_content" android:text="小龍還是壞蛋!"
???????????????????????????????????? ></TextView>
??????????????????????????? </LinearLayout>
?????????????????? </FrameLayout>
???????? </LinearLayout>
</TabHost>
?
?