Android Constraint Layout - Alignment of childrens

浪尽此生 提交于 2021-01-28 05:40:23

问题


So I have a constraint layout with height wrap_content and width match_parent. I would like to know

  1. How to align a button to the centre of a recycler view if they are rendered horizontally.

  2. How to align a button to the centre of a LinearLayout(which will be populated later).. if they are rendered horizontally.

I tried constraint.Guideline but it seems not working because of constraint layout wrap_content height. any suggestions?


回答1:


Maybe for other people this answer can be useful.

We can have an empty view below recycler view or linear layout.

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

now align button to the centre of parent top and this view.

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

This should do the work :)




回答2:


To centre align, you need to set left and right align with respect to parent. e.g.

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


来源:https://stackoverflow.com/questions/46213359/android-constraint-layout-alignment-of-childrens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!