首先 match_parent 與 fill_parent 功能是相同的,
但後面 Android 版本更新後會漸漸取消 fill_parent
故如果要用來填滿上層類別的部分,
還是建議使用 match_parent 在後期比較不會有問題.
再來就是 wrap_content 與 match_parent 的差異,
wrap_content:
就是被設定物件會依照內容物件(下層物件)的大小去做改變
match_parent:
就是被設定物件會依照上層物件(或父物件)的大小
填滿上層物件(或可以說符合上層物件改變自己的大小)
詳細介紹如下:
首先以四個元件做說明, 分別為LinearLayout1, button1,
button2, button3, 而 button 都被包含在 LinearLayout(紫框)
之內, (也就是 button 是 LinearLayout(紫框) 的內容物) 以下內容以
寬度 的設定去做變化.
下圖設定
LinearLayout(紫框) 設定:wrap_content
button1 設定:match_parent
button2 設定:wrap_content
button2 設定:wrap_content
由上圖結果可得知,
LinearLayout(紫框) 被設定為 wrap_content, 所以他的 寬度 會隨著他
最寬的內容物 button2 改變.
button1 被設定為 match_parent, 所以他的 寬度 會隨著他的
上層物(父物件) LinearLayout(紫框) 的 寬度 改變.
button2 被設定為 wrap_content, 所以他的 寬度 會隨著他的
內容物 文字 "Long Name Button2" 的長度 改變.
button3 被設定為 wrap_content, 所以他的 寬度 會隨著他的
內容物 文字 "Button3" 的長度 改變.
下圖設定
LinearLayout(紫框) 設定:match_parent <-- 與上圖不同
button1 設定:match_parent
button2 設定:wrap_content
button2 設定:wrap_content
由上圖結果可得知,
LinearLayout(紫框) 被設定為 match_parent, 所以他的 寬度 會隨著他
上層物(父物件) 設備的寬度 改變.
button1 被設定為 match_parent, 所以他的 寬度 會隨著他的
上層物(父物件) LinearLayout(紫框) 的 寬度 改變.
button2 被設定為 wrap_content, 所以他的 寬度 會隨著他的
內容物 文字 "Long Name Button2" 的長度 改變.
button3 被設定為 wrap_content, 所以他的 寬度 會隨著他的
內容物 文字 "Button3" 的長度 改變.
由上兩張圖就可以看出 wrap_content 與 match_parent 在設定上產生的差異
第一張圖將 LinearLayout(紫框) 設定 wrap_content,
button1 根據 LinearLayout(紫框) 改變,
LinearLayout(紫框) 根據 button2 改變,
button2 又根據 文字 "Long Name Button2" 改變.
而第二張圖將 LinearLayout(紫框) 設定改為 match_parent,
button1 根據 LinearLayout(紫框) 改變,
LinearLayout(紫框) 根據設備而改變,
button2 又根據 文字 "Long Name Button2" 改變.
留言列表