【问题标题】:Android Constraint Layout - Alignment of childrensAndroid 约束布局 - 子元素的对齐
【发布时间】:2017-09-14 07:46:51
【问题描述】:

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

  1. 如果button 水平呈现,如何将buttonrecycler view中心对齐。

  2. 如何将buttonLinearLayout(稍后将填充)的中心对齐.. 如果它们水平呈现。

我尝试了constraint.Guideline,但由于约束布局wrap_content 高度,它似乎不起作用。有什么建议吗?

【问题讨论】:

  • 在第一种情况下得到了结果,但在第二种情况下没有(线性布局)它具有固定高度

标签: android constraints


【解决方案1】:

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

我们可以在回收器视图或线性布局下方有一个空视图。

<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"

这应该可以完成工作:)

【讨论】:

    【解决方案2】:

    要居中对齐,您需要相对于父对象设置左右对齐。例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2020-06-28
      • 1970-01-01
      相关资源
      最近更新 更多