?
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
TabWidget
版本:Android 3.0 r1
?
結(jié)構
繼承關系
public class TabWidget extends LinearLayout
implements View.OnFocusChangeListener
????????
java.lang.Object
???????? android.view.View
?????????????????? android.view.ViewGroup
??????????????????????????? android.widget.LinearLayout
???????????????????????????????????? android.widget.TabWidget
?
類概述
顯示選項卡標簽的列表,用于代表父選項卡集合的每一頁。當前widget的容器對象是TabHost。當用戶選擇一個選項卡時,此對象給父容器對象TabHost發(fā)送一個消息,告訴TabHost切換到對應的頁面顯示。此對象上的很多方法,你通常不會直接使用。容器TabHost被用來添加標簽,添加并管理回調(diào)函數(shù)。你可能會調(diào)用此對象以迭代選項卡列表,或者調(diào)整選項卡列表的布局,但大多數(shù)方法應該是通過容器TabHost對象調(diào)用。
參見Tab Layout tutorial。
?
XML屬性
屬性名稱 |
描述 |
android:divider |
可繪制對象,被繪制在選項卡窗口間充當分割物。 |
android:tabStripEnabled |
確定是否在選項卡繪制 |
android:tabStripLeft |
被用來繪制選項卡下面的分割線左邊部分的可視化對象。 |
android:tabStripRight |
被用來繪制選項卡下面的分割線右邊部分的可視化對象。 |
?
公共方法
public void addView (View child)
增加子視圖。如果子視圖沒有設置布局參數(shù),那么將為子視圖設置此ViewGroup默認的布局參數(shù)。
參數(shù)
child????????? 新增的子視圖
?
public void childDrawableStateChanged (View child)
如果調(diào)用addStatesFromChildren函數(shù)返回true,刷新此組的可視化對象的狀態(tài)(包括它的子元素的狀態(tài))。
參數(shù)
child ???????? 可視化對象狀態(tài)被改變的子元素。
?
public void dispatchDraw (Canvas canvas)
繪制時調(diào)用此方法來繪制子視圖。派生類在被繪制之前,為獲取控制可能會重寫此方法(但必須在它自己的視圖已經(jīng)被繪制之后)。
參數(shù)
canvas????? 繪制視圖的畫布(canvas)
?
public void focusCurrentTab (int index)
設置當前選項卡并且讓其獲得焦點。此方法確保獲取焦點的選項卡窗口匹配選中的窗口,一般情況下類似 setCurrentTab(int)。通常當我們通過操作UI實現(xiàn)時,這些都不是問題,
因為UI負責調(diào)用TabWidget.onFocusChanged(),但如果我們通過程序控制選中選項卡窗口,那么我們就必須確認保持焦點。(譯者注:setCurrentTab(int)執(zhí)行后,選中指定索引的選項卡窗口,但不獲取焦點。focusCurrentTab(int)執(zhí)行后,不僅通過調(diào)用setCurrentTab(int)
選中指定索引的選項卡窗口還獲取焦點。)
參數(shù)
index???????? 你想要設置焦點(橘色高亮)并且選中(選項卡被至于部件widget的前端)的選項卡。
參見
setCurrentTab(int)
?
public View getChildTabViewAt (int index)
返回位于指定索引位置的選項卡標識符的視圖
參數(shù)
index???????? 選項卡指示符視圖返回的從零開始的索引
返回值
在提供的索引位置上的選項卡指示符視圖
?
public int getTabCount ()
返回選項卡的數(shù)量
返回值
選項卡指示符視圖的數(shù)量
?
public boolean isStripEnabled ()
指明選項卡指示符的底部分隔線是否繪制
?
public void onFocusChange (View v, boolean hasFocus)
當視圖的焦點狀態(tài)被改變時調(diào)用。
參數(shù)
v???????????????? 狀態(tài)變化的視圖
hasFocus 視圖的新焦點狀態(tài)。
?
???????? public void sendAccessibilityEventUnchecked (AccessibilityEvent event)
??? 處理用于發(fā)送AccessibilityEvent
的請求。
如果再發(fā)送之前調(diào)度事件,本
方法不保證可訪問性檢查??梢酝ㄟ^調(diào)用者調(diào)用isEnabled()
檢查。
?????????????????? 參數(shù)
??????????????????????????? event??????? 事件
?
public void setCurrentTab (int index)
設置當前選項卡。此方法被用來將選項卡置于widget的前部,并且通知其它的UI元素,一個不同的選項卡被調(diào)整到了前臺。注意,這不同于傳統(tǒng)的“焦點”,是從視圖邏輯的焦點。
例如,如果我們在選項卡式視圖中有一個列表,一個用戶可能會上下移動列表,通過列表選項移動UI焦點(橘色高亮)。光標的移動并不影響選項卡的選中狀態(tài),因為滾動
是在同一個選項卡上的。選中的選項卡僅僅當我們在選項卡上切換時(在此例中,從列表視圖移到下一選項卡視圖)更改。如果想要一次性的設置焦點并選中選項卡,請使用setCurrentTab(int)
。
正常情況下,視圖邏輯關注調(diào)整焦點,除非你規(guī)避了UI,你可能僅僅將你的興趣集中在這里。
參數(shù)
index????????????????? 你想要指做選中選項卡的選項卡索引(選項卡被顯示到widget的前部)
參見
focusCurrentTab(int)
?
public void setDividerDrawable (Drawable drawable)
設置用于在選項窗口指示符間分隔的可繪制對象
參數(shù)
drawable????????? 用于分隔的可繪制對象
?
public void setDividerDrawable (int resId)
設置用于在兩個選項卡標識符之間進行分隔的可繪制對象
參數(shù)
resId???????? ???????? 被用來分隔的可繪制對象的資源標識
?
public void setEnabled (boolean enabled)
設置當前視圖的啟用狀態(tài)。子類的不同而其對應的啟用狀態(tài)的解釋也不相同。
參數(shù)
enabled??? 是否啟動
?
public void setLeftStripDrawable (int resId)
設置被用來當作選項卡指示符下面的分割線,左邊部分的可繪制對象。
參數(shù)
resId???????? the?? 被用來當作左邊分隔線的可繪制對象的資源標識符
?
public void setLeftStripDrawable (Drawable drawable)
設置被用來當作選項卡指示符下面的分割線,左邊部分的可繪制對象。
參數(shù)
drawable 左邊分隔線的可繪制對象
?
public void setRightStripDrawable (int resId)
設置被用來當作選項卡指示符下面的分割線,右邊部分的可繪制對象。
參數(shù)
resId???????? ???????? 被用來當作右邊分隔線的可繪制對象的資源標識符
?
public void setRightStripDrawable (Drawable drawable)
設置被用來當作選項卡指示符下面的分割線,右邊部分的可繪制對象。
參數(shù)
drawable 右邊分隔線的可繪制對象
?
public void setStripEnabled (boolean stripEnabled)
控制選項卡指示符底部的分隔線是否繪制。默認是繪制。如果用戶為選項卡指示符指定一個定制的視圖,那么TabHost類調(diào)用此方法以禁用繪制底部分割線。
參數(shù)
stripEnabled??? 如果需要繪制底部分割線為true
?
受保護方法
protected int getChildDrawingOrder (int childCount, int i)
返回要繪制的子元素在當前迭代中的索引。如果你需要修改子元素的繪制順序,可以覆寫此方法。默認情況下返回參數(shù)i值。
注意:為使此方法被調(diào)用,你必須先通過調(diào)用setChildrenDrawingOrderEnabled(boolean)
啟用子元素排序
參數(shù)
i??????? ???????? 當前的迭代值
返回值
將要繪制的子元素在當前迭代的索引
?
protected void onSizeChanged (int w, int h, int oldw, int oldh)
布局過程中,當此視圖的大小發(fā)生改變時被調(diào)用的。如果你只是添加到視圖的層次結(jié)構,你調(diào)用時可以使用0當作之前的值(old value)。
參數(shù)
w????? ???????? 此視圖的當前寬度
h?????? ???????? 此視圖的當前高度
oldw????????? Old此視圖之前的寬度
oldh ???????? 此視圖之前的高度
?
補充
???????? 術語對照
indicator?????????? 指示符
drawable????????? 可繪制對象
iteration??????????? 迭代
strips???????????????? 分割線
文章鏈接
Android選項卡(TabWidget)例子
{Android學習指南}TabWidget
Android將TAB選項卡放在屏幕底部
示例代碼
???????? 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() + "個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>
?
?