Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android LinearLayout布局常用屬性完整整理

編輯:關於 Android 程式設計

LinearLayout 是最常被使用的 ViewGroup 之一,觀念清楚之後,多數簡單畫面都能快速搞定。以下把開發時最常碰到的屬性整理成一份對照表。

基本結構與排列方向

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> 
  • android:orientation:決定子元件排列方向。
  • vertical:垂直排列。
  • horizontal:水平排列。

垂直排列的寫法:

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" /> </LinearLayout> 

水平排列只要把 orientation 改為 horizontal 即可。

重心設定:android:gravity

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="left"> </LinearLayout> 

gravity 決定「容器內容物」的擺放方向,常用值有:

  • center:水平與垂直皆置中
  • center_vertical:垂直置中
  • center_horizontal:水平置中
  • top:置頂
  • bottom:置底
  • left:置左
  • right:置右

水平置中與垂直置中的範例:

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> 

用 OR 運算子組合重心

gravity 可以用「|」串接多個值,做出對角或雙軸置中的效果:

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="top|right"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="bottom|left"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical|center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> 

比例分配:android:layout_weight

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_weight="1"/> </LinearLayout> 
  • android:layout_weight:子元件或子框架所佔的比重。
  • 只有在 LinearLayout 底下的直接子元件才能設定這個屬性。

等比例分配——三顆按鈕比重都是 1,寬度就相同:

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_weight="1"/> </LinearLayout> 

非等比例分配——比重相加為 1,正好對應 100%:

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_weight=".10"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_weight=".20"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_weight=".70"/> </LinearLayout> 

這裡的 .10、.20、.70 分別代表 0.1、0.2、0.7,合計剛好 1,也就是 10%、20%、70% 的配置。

實務提醒

寫 layout_weight 時,建議把該子元件的 layout_width 設為 0dp(而非 wrap_content),可以避免多餘的量測與奇怪的佔位問題。另外,層數過深的 LinearLayout 嵌套會拖慢渲染效能,畫面複雜時值得評估改用 ConstraintLayout。

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