要建立一個 TableLayout,需要注意的第一點,

是必須有相對應的 XML 檔案。

 

像是這樣:

tableLayout_1  

然後可以在 Activity 下,去創建所需的 TableLayout:

tableLayout_2  

可以創造出這樣的 TableLayout:

tableLayout_3  

這是最基本的 TableLayout,一般都是使用 TextView 作為欄位,

直接 addView(TextView),TableLayout 就會自動幫我們安排適當大小,

一個一個填入適當位置。

 

但如果有特殊需求,需要利用 FrameLayout,

或者 LinearLayout 等作為欄位呢?

 

這也是可行的喔!

 

tableLayout_4    

會產生這樣的效果:

tableLayout_5  

這裡需要注意的地方是,如果作為欄位的 LinearLayout 

設定了 LayoutParams,會怎麼樣呢?

tableLayout_6  

 

卻發生這樣的結果:

tableLayout_7  

因為 TableRow 是自動去符合內容的欄位,

它的寬度永遠是 fill_parent, 高度永遠是 wrap_content。

在 Google 的 Android Developer 網頁上有此說明:

 

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

 

所以想要使用LinearLayout 或者 FrameLayout

作為欄位的話,千萬不要去設定 LayoutParams,

否則就會什麼都看不到喔!!

 

 

arrow
arrow
    全站熱搜

    keep walking 發表在 痞客邦 留言(0) 人氣()