都8102年了芽唇,趕緊來學(xué)一下約束布局
ConstraintLayout 約束布局谅海。約束貫穿這個布局的使用方式滚婉。
定位位置
要訣: 其實就是哪一條邊與哪一條邊對齊凰狞。
例如:讓一個button居中豁陆,可以為它添加layout_contrainLeft_toLeftOf
柑爸、layout_contrainRight_toRightOf
、
layout_contrainTop_toTopOf
layout_contrainBottom_toBottomOf
這4個約束盒音。
layout_xxx_to_leftOf與layout_xxx_to_StartOf類似表鳍,同理右邊也是一樣。
- layout_contraintLeft_toLeftOf
- layout_constraintLeft_toRightOf
- layout_constraintRight_toLeftOf
- layout_constraintRight_toRightOf
- layout_constraintTop_toTopOf
- layout_constraintTop_toBottomOf
- layout_constraintBottom_toTopOf
- layout_constraintBottom_toBottomOf
- layout_constraintBaseline_toBaselineOf
- layout_constraintStart_toEndOf
- layout_constraintStart_toStartOf
- layout_constraintEnd_toStartOf
- layout_constraintEnd_toEndOf
layout_goneXXX
在ContraintLayout中常用的android:layout_marginXXX
依然可用祥诽。除了這些ContraintLayout還添加了當(dāng)被依賴的約束對象的visibility
設(shè)置為gone
時的位置屬性譬圣。
- layout_goneMarginStart
- layout_goneMarginEnd
- layout_goneMarginLeft
- layout_goneMarginRight
- layout_goneMarginTop
- layout_goneMarginBottom
layout_constrainHorizontal_bias / layout_constrainVertical_bias
當(dāng)控件設(shè)置為橫向約束(layout_contraintLeft_toLeftOf
與layout_contraintRight_toRightOf
)或者縱向約束(layout_contraintTop_toTopOf
與layout_contraintBottom_toBottomOf
)的時候,以上屬性才會生效雄坪。
該屬性的范圍是 0.0 -----1.0 ,當(dāng)為0.5時厘熟,使用該屬性的控件在方向上居中。
控件大小
在ConstraintLayout中维哈,不支持
layout_width
和layout_height
取值為match_parent
取值:
- wrap_content
- 具體的dp值
- 0dp(match_constraint)
注意:
- 0dp的意思不是
match_parent
绳姨,是填充約束。比如一個控件添加了橫向的約束阔挠,它的layout_width
取值為0dp時飘庄,表示占滿橫向。 - 當(dāng)控件為wrap_content可以使用
app:layout_constrainedWidth=”true|false”
购撼,
app:layout_constrainedHeight=”true|false”
來限制控件是否會超過約束限制跪削。
app:layout_constraintWidth_percent`
當(dāng)控件的寬度為0dp時,此屬性代表占父布局的的百分比(0.5即占父布局的一半)
layout_constraintDimentionRatio
設(shè)置控件的寬高比迂求,此屬性要在控件的
layout_width
設(shè)置為0dp時才有用碾盐。
代替LineartLayout
用layout_contraintXXX_toXXXOf
就可以實現(xiàn)。
水平方向:在第一個控件設(shè)置layout_constraintHorizontal_chainStyle
垂直方向:同樣在第一個控件設(shè)置layout_constraintVertical_chainStyle
取值效果如下:
圓形布局
- layout_constraintCircle : 圓心揩局,值是某個view的id
l- ayout_constraintCircleRadius : 半徑 - layout_constraintCircleAngle :角度廓旬,值是從0-360,0是指正上方-
Group
可以同時控制多個view可見性的虛擬view
<android.support.constraint.Group
...
android:visibility="invisible"
app:constraint_referenced_ids="a,b" />
<android.support.constraint.Group
...
android:visibility="visible"
app:constraint_referenced_ids="c,d" />
比如有a谐腰、b孕豹、c 和 d四個textview使用group可以同時改變他們的可見性。