我有两个视图 - A 和 B。它们有不同的高度。
如何在内部垂直对齐这些视图的中心ConstraintLayout
?
例如,在下面的 XML 中,我想要中心img_change_picture
与中心对齐txt_change_picture
.
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.constraint.Guideline
android:id="@+id/guideline_icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1"/>
<android.support.constraint.Guideline
android:id="@+id/guideline_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2"/>
<ImageView
android:id="@+id/img_change_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/guideline_icons"
app:layout_constraintTop_toBottomOf="@id/img_header"
android:layout_marginTop="@dimen/settings_main_vertical_spacing"
app:srcCompat="@drawable/change_picture"/>
<TextView
android:id="@+id/txt_change_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/guideline_text"
android:text="@string/settings_main_change_picture"
/>
</android.support.constraint.ConstraintLayout>
-
如果没有指南,请将属性放在需要垂直对齐的每个视图上。
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
-
有指导
添加水平辅助线并使其垂直居中layout_constraintGuide_percent
.
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
对于每个视图,将它们锚定到带有属性的指南
app:layout_constraintTop_toTopOf="@id/guideline"
app:layout_constraintBottom_toBottomOf="@id/guideline"
使用参考线,您将拥有更大的灵活性,因为您可以通过移动参考线轻松更改所有视图位置。
Update:
要将一个视图与另一个视图垂直居中对齐,您只需在属性中引用视图 id 即可。对齐txt_change_picture
垂直居中img_change_picture
,您可以像这样更改布局:
<TextView
android:id="@+id/txt_change_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/guideline_text"
android:text="@string/settings_main_change_picture"
app:layout_constraintTop_toTopOf="@id/img_change_picture"
app:layout_constraintBottom_toBottomOf="@id/img_change_picture"
/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)