问题
So I have a constraint layout with height wrap_content
and width match_parent
. I would like to know
How to align a
button
to the centre of arecycler view
if they are rendered horizontally.How to align a
button
to the centre of aLinearLayout
(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