Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 中文 API(119)——TableRow 類別詳解

編輯:Android 開發實例

類別定義

public class TableRow extends LinearLayout

繼承鏈:java.lang.Object → android.view.View → android.view.ViewGroup → android.widget.LinearLayout → android.widget.TableRow

類別說明

TableRow 是一個以水平方向排列子檢視的版面容器,設計上是作為 TableLayout 的子檢視使用,用來表示表格中的一列。若它的父容器並非 TableLayout,則行為等同於一個水平方向的 LinearLayout。

一個實用特性:在 XML 中配置 TableRow 的子檢視時,不需要(也不應該)指定 layout_width 與 layout_height。TableRow 會強制把這兩個屬性分別設為 MATCH_PARENT 與 WRAP_CONTENT。

關於欄位跨度等進階設定,請參考 android.widget.TableRow.LayoutParams。

內部類別

TableRow.LayoutParams——供 TableRow 的子檢視使用的版面參數。

建構子

TableRow(Context context)

以指定的上下文建立 TableRow。context 為應用程式環境。

TableRow(Context context, AttributeSet attrs)

以指定的上下文與屬性集建立 TableRow。attrs 為屬性集合。

公開方法

generateLayoutParams(AttributeSet attrs)

依提供的屬性集產生一組版面參數,回傳 ViewGroup.LayoutParams 或其子類別的實例。

getVirtualChildAt(int i)

取得指定索引的子檢視。實作時需將虛例子檢視(由 TableLayout 的欄位填補機制產生)一併納入考量,可參考 TableLayout 與 TableRow 的範例程式。

getVirtualChildCount()

回傳虛例子檢視的數量。由於這個數字可能包含為對齊而插入的佔位欄位,因此不一定等於實際加入的子檢視數。

setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener)

註冊一個回呼,在子檢視被加入或移除時觸發。

受保護方法

checkLayoutParams(ViewGroup.LayoutParams p)

檢查傳入的參數是否為本容器可接受的型別。

generateDefaultLayoutParams()

回傳預設版面參數:寬度 MATCH_PARENT、高度 WRAP_CONTENT、無欄位跨度。

generateLayoutParams(ViewGroup.LayoutParams p)

當父容器傳入的版面參數未通過 checkLayoutParams() 檢查時呼叫,負責轉換為一組適用於當前容器的參數,必要時會從來源複製可用屬性。

onLayout(boolean changed, int l, int t, int r, int b)

在容器為子檢視決定尺寸與位置時呼叫,子類別可覆寫以自訂排列邏輯。參數分別代表是否變更、以及相對父容器的左、上、右、下邊界。

onMeasure(int widthMeasureSpec, int heightMeasureSpec)

由 measure(int, int) 呼叫,用來量測檢視及其內容的寬高。覆寫時有兩條硬性約定:

  • 必須呼叫 setMeasuredDimension(int, int) 儲存量測結果,否則 measure() 會拋出 IllegalStateException。
  • 量測出的寬高不得小於檢視建議的最小寬高(getSuggestedMinimumWidth() 與 getSuggestedMinimumHeight())。

基底類別的實作以背景尺寸為預設大小(除非 MeasureSpec 允許更大),因此子類別通常仍需覆寫以提供更貼近內容的解。兩個參數皆由 View.MeasureSpec 編碼,分別代表父容器要求的橫向與縱向空間。

TableRow.LayoutParams

繼承鏈:ViewGroup.LayoutParams → ViewGroup.MarginLayoutParams → LinearLayout.LayoutParams → TableRow.LayoutParams。用來描述表格列中每個儲存格的位置與跨度。

XML 屬性

屬性說明
android:layout_column該子元素所在的欄索引,必須為整數,例如 "100"。
android:layout_span該子元素跨越的欄數,必須大於等於 1,且為整數。

欄位

  • public int column:儲存格在元件上對應的欄索引。
  • public int span:元件跨越的欄數。

建構子

  • TableRow.LayoutParams(Context c, AttributeSet attrs)
  • TableRow.LayoutParams(int w, int h):指定子元素的寬與高。
  • TableRow.LayoutParams(int w, int h, float initWeight):在寬高之外指定初始權重。
  • TableRow.LayoutParams():寬度為 ViewGroup.LayoutParams、高度為 WRAP_CONTENT。
  • TableRow.LayoutParams(int column):指定所在欄位,寬度 MATCH_PARENT、高度 WRAP_CONTENT。
  • TableRow.LayoutParams(ViewGroup.LayoutParams p) 與 TableRow.LayoutParams(ViewGroup.MarginLayoutParams source):由既有參數複製建構。

setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)

從提供的樣式屬性中取出版面參數。a 為要讀取的屬性來源,widthAttr 與 heightAttr 分別是寬度與高度屬性的識別碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved