a284628487 / AndroidPoint

Android Note
1 stars 0 forks source link

ConstraintLayout #10

Open a284628487 opened 6 years ago

a284628487 commented 6 years ago

https://developer.android.com/reference/android/support/constraint/ConstraintLayout

a284628487 commented 6 years ago

Relative positioning

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。

Margins

android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom

margin只能是正数值,或者是0。

Margins when connected to a GONE widget

当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

Centering positioning and bias

Centering 居中

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

Bias

app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintVertical_bias

默认情况下app:layout_constraintRight_toRightOfapp:layout_constraintRight_toRightOf都设置为parent,指定了View在Layout中水平居中,左右边距都是50%。 可以通过设置app:layout_constraintHorizontal_bias来设置左边距的百分比。

Circular positioning

通过圆环半径及角度,来指定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是指导buttonAbuttonB的中心位置的距离。

Visibility behavior

通常,标记为View.GONE的View,将不再显示,并且不再是layout中的一部分。但是ConstraintLayout对于View.GONE属性,有特殊的处理。GONE属性标记的View,仍然属于layout中的一部分。

width & height

Widgets dimension constraints

ConstraintLayout中不推荐使用MATCH_PARENT,可以使用 MATCH_CONSTRAINT 来进行替换。

Percent dimension

要使用Percent的步骤:

Ratio

指定横竖比例,至少需要指定一个尺寸(width或者height)为0dp,然后指定 layout_constraintDimensionRatio 属性。

<Button android:layout_width="wrap_content"
  android:layout_height="0dp"
  app:layout_constraintDimensionRatio="1:1" />

该值可以指定为两种: