1. 程式人生 > >一分鐘學會 ConstraintLayout 之從屬性角度理解布局

一分鐘學會 ConstraintLayout 之從屬性角度理解布局

uic mem app tor normal add 部分 pla ray

ConstraintLayout

在 Android 開發中,我們通常是手寫布局,很少會用拖動來寫布局,雖然 ConstraintLayout 在 I/O 上以拖動來展現了各種功能,我估計在以後開發中,程序員還是習慣手擼代碼。

我自己試著拖著用了一下,用得不是很明白 ,而且用起來效果不是很好。

那麽
直接上手擼了一下~~~

其實很簡單

技術分享

技術分享

Button1:app:layout_constraintBottom_toTopOf="@id/iv_head"

我們把這個屬性拆開來看, constraintBottom 指的本身的底部,即 Button1 的頂部, toTopOf 是指 ImageView 的頂部,那麽這句話的意思就是

Aligns the bottom of the desired view to the top of another.(官方原文)
翻譯一下就是 Button1 的底部要和 ImageView 的頂部對齊

Button1 app:layout_constraintRight_toLeftOf="@id/iv_head"
根據上面的規則我們就知道 Button1 的右邊要和 ImageView 的左邊對齊。

其實很簡單就是說兩個 View 的某個方位要對齊

沒了,就這麽簡單,其它屬性可以舉一反三,它比 RelativeLayout 控制起來更加得以就手。

  • layout_constraintTop_toTopOf ?—? Align the top of the desired view to the top of another.
  • layout_constraintTop_toBottomOf ?—? Align the top of the desired view to the bottom of another.
  • layout_constraintBottom_toTopOf ?—? Align the bottom of the desired view to the top of another.
  • layout_constraintBottom_toBottomOf ?—? Align the bottom of the desired view to the bottom of another.
  • layout_constraintLeft_toTopOf ?—? Align the left of the desired view to the top of another.
  • layout_constraintLeft_toBottomOf ?—? Align the left of the desired view to the bottom of another.
  • layout_constraintLeft_toLeftOf ?—? Align the left of the desired view to the left of another.
  • layout_constraintLeft_toRightOf ?—? Align the left of the desired view to the right of another.
  • layout_constraintRight_toTopOf ?—? Align the right of the desired view to the top of another.
  • layout_constraintRight_toBottomOf ?—? Align the right of the desired view to the bottom of another.
  • layout_constraintRight_toLeftOf ?—? Align the right of the desired view to the left of another.
  • layout_constraintRight_toRightOf ?—? Align the right of the desired view to the right of another.
  • If desired, attributes supporting start and end are also available in place of left and right alignment.

到此,你已經掌握了一大半的 ConstraintLayout 知識點

還有其它的一些屬性
app:layout_constraintStart_toEndOf
意思就是 Button 的開始部分(從左往右看,開始部分就是 Button 的左邊)與 ImageView 的右邊是對齊的。
app:layout_constraintStart_toStartOf
這個就是說 Button 的左邊與 ImageView 的左邊是對齊的

不知道為什麽上面已經出的屬性能夠滿足布局需要了,為什麽還要再出 start 和 end 的。

原文:

一分鐘學會 ConstraintLayout

http://chenxiaojian.net/quickly-learn-constraintlayout.html

the desired view align 2 回復 | 直到 2016-06-24 11:17:32 +08:00
技術分享 1 jianqiu 2016-06-24 10:04:21 +08:00 start 和 end 都是針對某些國家習慣從右到左布局的..保持這種兼容性
技術分享 2 ChenPing 2016-06-24 11:17:32 +08:00 @jianqiu 原來如此

一分鐘學會 ConstraintLayout 之從屬性角度理解布局