Android约束布局 - 儿童的对齐

时间:2017-09-14 07:46:52

标签: android constraints

所以我有一个高度为wrap_content和宽度为match_parent的约束布局。我想知道

  1. 如果button水平渲染,recycler viewbutton中心对齐。

  2. 如何将LinearLayoutconstraint.Guideline中心对齐(稍后将填充)..如果它们是水平渲染的。

  3. 我尝试了wrap_content,但由于约束布局{{1}}高度,它似乎无效。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

要居中对齐,您需要相对于父级设置左右对齐。 e.g。

<Button
    android:id="@+id/button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

答案 1 :(得分:0)

对于其他人来说,这个答案可能很有用。

我们可以在回收者视图或线性布局下面看到一个空视图。

<View
        android:id="@+id/horizontal_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerview"
        tools:layout_editor_absoluteX="8dp" />

现在将按钮对齐到父顶部和此视图的中心。

app:layout_constraintBottom_toTopOf="@+id/horizontal_view"
app:layout_constraintLeft_toLeftOf="@+id/parent"
app:layout_constraintRight_toLeftOf="recyclerview"
app:layout_constraintTop_toTopOf="parent"

这应该做的工作:)