【发布时间】:2020-05-01 20:12:02
【问题描述】:
我有两个 TextView,一个在另一个之上。我希望两个 TextView 的垂直中间位置与 ImageView 的垂直中间位置相同。 (就是这样,无论文本视图中可能有多少文本,所有内容都将始终看起来整齐、垂直。)
我使用两个 LinearLayout 完美地创建了我需要的东西(因为标题上方的空间与描述下方的空间相同):
但 Android Studio 无法成功地将其转换为 ConstraintLayout,因为它只是将 TextViews 转储到了布局的底部。我玩过很多属性,但无法完全达到所需的布局。
我的问题与this one 类似,只是我试图将 pair 视图而不是单个视图居中对齐 - 这意味着我没有视图边缘对齐到 ImageView/容器的中心。
是否可以使用 ConstraintLayout 实现我的目标? (我希望我可以用一个 RelativeLayout 来做到这一点,但我想在我的 ImageView 上使用 layout_constraintDimensionRatio 属性,这大概让我需要使用 ConstraintLayout。)
如果有帮助,这里是我前面提到的 LinearLayout 的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="@dimen/resources_list_image_size"
android:layout_height="@dimen/resources_list_image_size"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_gravity="center_vertical"
android:contentDescription="@string/resource_image"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/MyTextAppearanceMedium"
tools:text="Title" />
<TextView
android:id="@+id/textViewDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/MyTextAppearanceSmall"
tools:text="Description" />
</LinearLayout>
</LinearLayout>
更新:解决方案
感谢Ben P's answer,这是我的最终代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<!-- Add guideline to align imageView to. -->
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.3" />
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/resource_image"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="@+id/textViewTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
app:layout_constraintBottom_toTopOf="@id/textViewDescription"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageView"
app:layout_constraintTop_toTopOf="parent"
android:textAppearance="@style/MyTextAppearanceMedium"
app:fontFamily="@font/roboto_slab_regular"
app:layout_constraintVertical_chainStyle="packed"
tools:text="@string/enter_title_colon" />
<TextView
android:id="@+id/textViewDescription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageView"
app:layout_constraintTop_toBottomOf="@id/textViewTitle"
app:fontFamily="@font/roboto_slab_light"
android:textAppearance="@style/MyTextAppearanceSmall"
tools:text="Description" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
您可以根据指南进行尝试。该准则也必须是从上到上的图像视图和从下到下的图像视图的约束。那么第一个文本视图必须是约束底部到指南,第二个文本视图必须是约束顶部到指南。
标签: android android-layout android-constraintlayout