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

搞懂 Android 的置中:Button 與 Gallery 為什麼會對齊

編輯:開發入門

從上圖可以看到,Button 與 Gallery 採用的對齊方式是「置中對齊」,也就是 Button 與它的 Parent 容器中心對齊,而不是貼齊某個邊緣。

Android 在對齊這件事上提供了好幾個層次不同的設定,官方文件對這幾個值的定義如下:

  • CENTER_VERTICAL:Place object in the vertical center of its container, not changing its size. 把物件放在容器的垂直中心,不改變其尺寸。
  • bottom:Push object to the bottom of its container, not changing its size. 把物件推到容器底部,同樣不改變尺寸。
  • android:gravity="center":Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. 在垂直與水平兩個軸向上都置中。

換句話說,這幾個值的差別只在「對齊哪個軸」以及「靠哪個邊」,它們都不會改變元件本身的寬高。真正會撐開或壓縮元件尺寸的是 weight 這類機制,別把兩者搞混了。

實務上常見的誤解是:以為設了 gravity 元件就會自動填滿或縮放。事實上元件尺寸仍由 layout_width 與 layout_height 決定,gravity 只負責在既有空間裡安排它的位置。

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