正文
一、結構
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發送一個消息,告訴TabHost切換到對應的頁面顯示。此對象上的很多方法,你通常不會直接使用。容器TabHost被用來添加标簽,添加并管理回調函數。你可能會調用此對象以疊代頁籤清單,或者調整頁籤清單的布局,但大多數方法應該是通過容器TabHost對象調用。
三、XML屬性
屬性名稱
描述
android:divider
可繪制對象,被繪制在頁籤視窗間充當分割物。
android:tabStripEnabled
确定是否在頁籤繪制
android:tabStripLeft
被用來繪制頁籤下面的分割線左邊部分的可視化對象。
android:tabStripRight
被用來繪制頁籤下面的分割線右邊部分的可視化對象。
四、公共方法
public void addView (View child)
增加子視圖。如果子視圖沒有設定布局參數,那麼将為子視圖設定此ViewGroup預設的布局參數。
參數
child 新增的子視圖
public void childDrawableStateChanged (View child)
如果調用addStatesFromChildren函數傳回true,重新整理此組的可視化對象的狀态(包括它的子元素的狀态)。
child 可視化對象狀态被改變的子元素。
public void dispatchDraw (Canvas canvas)
繪制時調用此方法來繪制子視圖。派生類在被繪制之前,為擷取控制可能會重寫此方法(但必須在它自己的視圖已經被繪制之後)。
canvas 繪制視圖的畫布(canvas)
public void focusCurrentTab (int index)
設定目前頁籤并且讓其獲得焦點。此方法確定擷取焦點的頁籤視窗比對選中的視窗,一般情況下類似 setCurrentTab(int)。通常當我們通過操作UI實作時,這些都不是問題,因為UI負責調用TabWidget.onFocusChanged(),但如果我們通過程式控制選中頁籤視窗,那麼我們就必須确認保持焦點。(譯者注:setCurrentTab(int)執行後,選中指定索引的頁籤視窗,但不擷取焦點。focusCurrentTab(int)執行後,不僅通過調用setCurrentTab(int)選中指定索引的頁籤視窗還擷取焦點。)
index 你想要設定焦點(橘色高亮)并且選中(頁籤被至于部件widget的前端)的頁籤。
參見
public View getChildTabViewAt (int index)
傳回位于指定索引位置的頁籤辨別符的視圖
index 頁籤訓示符視圖傳回的從零開始的索引
傳回值
在提供的索引位置上的頁籤訓示符視圖
public int getTabCount ()
傳回頁籤的數量
傳回值
頁籤訓示符視圖的數量
public boolean isStripEnabled ()
指明頁籤訓示符的底部分隔線是否繪制
public void onFocusChange (View v, boolean hasFocus)
當視圖的焦點狀态被改變時調用。
v 狀态變化的視圖
hasFocus 視圖的新焦點狀态。
public void sendAccessibilityEventUnchecked (AccessibilityEvent event)
參數
event 事件
public void setCurrentTab (int index)
設定目前頁籤。此方法被用來将頁籤置于widget的前部,并且通知其它的UI元素,一個不同的頁籤被調整到了前台。注意,這不同于傳統的“焦點”,是從視圖邏輯的焦點。
正常情況下,視圖邏輯關注調整焦點,除非你規避了UI,你可能僅僅将你的興趣集中在這裡。
index 你想要指做選中頁籤的頁籤索引(頁籤被顯示到widget的前部)
public void setDividerDrawable (Drawable drawable)
設定用于在選項視窗訓示符間分隔的可繪制對象
drawable 用于分隔的可繪制對象
public void setDividerDrawable (int resId)
設定用于在兩個頁籤辨別符之間進行分隔的可繪制對象
resId 被用來分隔的可繪制對象的資源辨別
public void setEnabled (boolean enabled)
設定目前視圖的啟用狀态。子類的不同而其對應的啟用狀态的解釋也不相同。
enabled 是否啟動
public void setLeftStripDrawable (int resId)
設定被用來當作頁籤訓示符下面的分割線,左邊部分的可繪制對象。
resId the 被用來當作左邊分隔線的可繪制對象的資源辨別符
public void setLeftStripDrawable (Drawable drawable)
drawable 左邊分隔線的可繪制對象
public void setRightStripDrawable (int resId)
設定被用來當作頁籤訓示符下面的分割線,右邊部分的可繪制對象。
resId 被用來當作右邊分隔線的可繪制對象的資源辨別符
public void setRightStripDrawable (Drawable drawable)
drawable 右邊分隔線的可繪制對象
public void setStripEnabled (boolean stripEnabled)
控制頁籤訓示符底部的分隔線是否繪制。預設是繪制。如果使用者為頁籤訓示符指定一個定制的視圖,那麼TabHost類調用此方法以禁用繪制底部分割線。
stripEnabled 如果需要繪制底部分割線為true
五、受保護方法
protected int getChildDrawingOrder (int childCount, int i)
傳回要繪制的子元素在目前疊代中的索引。如果你需要修改子元素的繪制順序,可以覆寫此方法。預設情況下傳回參數i值。
i 目前的疊代值
将要繪制的子元素在目前疊代的索引
protected void onSizeChanged (int w, int h, int oldw, int oldh)
布局過程中,當此視圖的大小發生改變時被調用的。如果你隻是添加到視圖的層次結構,你調用時可以使用0當作之前的值(old value)。
w 此視圖的目前寬度
h 此視圖的目前高度
oldw Old此視圖之前的寬度
oldh 此視圖之前的高度
六、補充
術語對照
indicator 訓示符
drawable 可繪制對象
iteration 疊代
strips 分割線
文章精選
示例代碼
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");
}
public void onClick(View v) {
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);
default:
}
}
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="顯示狀态切換"
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="分隔線狀态切換"></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"
<TextView android:id="@+id/TextView2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="小龍是壞蛋!"></TextView>
<LinearLayout android:id="@+id/LinearLayout003"
<TextView android:id="@+id/TextView3" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="小龍還是壞蛋!"></TextView>
</FrameLayout>
</LinearLayout>
</TabHost>
結束
每次合集釋出就像受檢部隊在接受檢閱,緊張而又榮耀。為了每次的檢閱,大家盡心盡力的籌備,做同一件事情,鼓勵自己堅持,鼓勵“戰友”堅持。緻力于可持續發展開源事業的建設。
本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/582358,如需轉載請自行聯系原作者