ConstraintLayout - 如何垂直对齐两个视图的中心
ConstraintLayout - how to align centers of two views vertically
我有两个视图 - 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>
没有 Guideline,将属性放在需要垂直对齐的每个视图上。
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
有指南
添加水平 Guideline 并使其垂直居中 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"
有了辅助线,您将拥有更大的灵活性,因为您可以通过移动辅助线轻松更改所有视图的位置。
更新:
要将一个视图与另一个视图垂直居中对齐,您只需在属性中引用视图 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"
/>
Android Studio 3.5
如果是 TL;TR...
Select 您想要的观看次数,右键 :
你会得到:
我有两个视图 - 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>
没有 Guideline,将属性放在需要垂直对齐的每个视图上。
app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"
有指南
添加水平 Guideline 并使其垂直居中 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"
有了辅助线,您将拥有更大的灵活性,因为您可以通过移动辅助线轻松更改所有视图的位置。
更新:
要将一个视图与另一个视图垂直居中对齐,您只需在属性中引用视图 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"
/>
Android Studio 3.5
如果是 TL;TR...
Select 您想要的观看次数,右键
你会得到: