Open a284628487 opened 6 years ago
app:layout_constraintTop_toBottomOf="@id/targetView"
// 当前View的Top位置在指定的targetView的Bottom,即在targetView的下边
app:layout_constraintLeft_toLeftOf="@id/targetView"
// 当前View的Left位置在指定的targetView的Left,即垂直方向上和targetView左对齐
app:layout_constraintTop_toTopOf="@id/targetView"
// 当前View的Top位置在指定的targetView的Top,即水平方向上和targetView水平对齐
app:layout_constraintLeft_toRightOf="@id/targetView"
// 当前View的Left位置在指定的targetView的Right,即在targetView的右边
可以预见的是,还有至少4组给定的属性,不再列出。
除了left/top/rigit/bottom之外,还有以下几组:
```xml
app:layout_constraintBaseline_toBaselineOf
app:layout_constraintStart_toEndOf
app:layout_constraintStart_toStartOf
app:layout_constraintEnd_toStartOf
app:layout_constraintEnd_toEndOf
这些所有的属性,都引用另外一个View或者是parent。
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
margin只能是正数值,或者是0。
当constraint的targetView的visibility属性为View.GONE时,如果是在RelativeLayout中,则效果消失,但是在ConstraintLayout中,可以使用下面的属性来避免依赖消失的情况。
app:layout_goneMarginStart
app:layout_goneMarginEnd
app:layout_goneMarginLeft
app:layout_goneMarginTop
app:layout_goneMarginRight
app:layout_goneMarginBottom
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintVertical_bias
默认情况下app:layout_constraintRight_toRightOf
和app:layout_constraintRight_toRightOf
都设置为parent
,指定了View在Layout中水平居中,左右边距都是50%。
可以通过设置app:layout_constraintHorizontal_bias
来设置左边距的百分比。
通过圆环半径及角度,来指定View的位置。
app:layout_constraintCircle : references another widget id
app:layout_constraintCircleRadius : the distance to the other widget center
app:layout_constraintCircleAngle : which angle the widget should be at (in degrees, from 0 to 360)
比如:
<Button android:id="@+id/buttonA" ... />
<Button android:id="@+id/buttonB" ...
app:layout_constraintCircle="@+id/buttonA"
app:layout_constraintCircleRadius="100dp"
app:layout_constraintCircleAngle="45" />
buttonB
将在buttonA
的右上角45度角度100dp
处,100dp
是指导buttonA
和buttonB
的中心位置的距离。
通常,标记为View.GONE的View,将不再显示,并且不再是layout中的一部分。但是ConstraintLayout对于View.GONE
属性,有特殊的处理。GONE属性标记的View,仍然属于layout中的一部分。
在
ConstraintLayout
中不推荐使用MATCH_PARENT,可以使用 MATCH_CONSTRAINT 来进行替换。
要使用Percent的步骤:
MATCH_CONTRAINT
app:layout_constraintWidth_default="percent"
或者 app:layout_constraintHeight_default="percent"
layout_constraintWidth_percent
或者 layout_constraintHeight_percent
为0到1指定横竖比例,至少需要指定一个尺寸(width或者height)为0dp,然后指定 layout_constraintDimensionRatio 属性。
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />
该值可以指定为两种:
app:layout_constraintDimensionRatio="W,16:9"
指定基准边为W,则以W进行基准缩放。此种情况下,则通过调整W,以满足达到1:1的比例。
https://developer.android.com/reference/android/support/constraint/ConstraintLayout