约束布局垂直居中对齐 - 两个 Child 视图

Constraint Layout Vertical Align Center - Two Child Views

我有两个 TextView,一个在另一个上面。我希望两个 TextView 的垂直中间位置与 ImageView 的垂直中间位置相同。 (就是这样,无论可能进入任一 TextView 的文本数量如何,所有内容都将始终看起来整齐、垂直。)

我使用两个 LinearLayout 完美地创建了我需要的东西(因为标题上方的 space 与描述下方的 space 相同) :

但是 Android Studio 无法将其成功转换为 ConstraintLayout,因为它只是将 TextView 转储到布局底部。我尝试了很多属性,但无法完全达到所需的布局。

我的问题类似于 ,只是我试图 center_vertical 对齐 视图而不是单个视图 - 这意味着我没有视图边缘与 ImageView/container.

的中心对齐

是否可以通过 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>

更新:解决方案

感谢,这是我的最终代码:

<?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>

您可以使用这种布局:

<?xml version="1.0" encoding="utf-8"?>
<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">

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorAccent"
    android:text="I am 5% of the screen height"
    app:layout_constraintBottom_toTopOf="@+id/textView3"
    app:layout_constraintEnd_toEndOf="@+id/textView3"
    app:layout_constraintHeight_percent="0.05"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="@+id/textView3"
    app:layout_constraintTop_toTopOf="@+id/imageView2" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_percent="0.15"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:background="@color/colorPrimary"
    android:text="I am 15% of the screen height (And the image is 20% screen size in height) "
    app:layout_constraintBottom_toBottomOf="@+id/imageView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageView2"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_percent="0.20"
    app:layout_constraintDimensionRatio="1:1"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/rose" />

</android.support.constraint.ConstraintLayout>

看起来像这样:

关于此布局的一件重要事情:

您可以使用 app:layout_constraintDimensionRatio="x:y" 控制您的宽高比(对于图像),并通过传递“1:1”使其成为正方形

顺便说一句——我在这个例子中无缘无故地使用了支持库,你可以使用 androidx

如果你想使用 ConstraintLayout 你可以使用这样的东西:

<?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="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:contentDescription="description"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="@+id/imageView">

        <TextView
            android:id="@+id/textViewTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Title" />

        <TextView
            android:id="@+id/textViewDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Description" />

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

听起来您可以通过使用锚定到 ImageView 顶部和底部的 打包链 来解决此问题。您还需要使用水平偏差和 约束宽度 才能使环绕正常工作。

<?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="match_parent">

    <View
        android:id="@+id/anchor"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_margin="64dp"
        android:background="#caf"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="@id/anchor"
        app:layout_constraintStart_toEndOf="@id/anchor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/two"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toBottomOf="@id/one"
        app:layout_constraintStart_toEndOf="@id/anchor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/anchor"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这里的重要属性是:

  • app:layout_constraintVertical_chainStyle="packed" 在第一个视图上,这导致两个文本视图堆叠在彼此之上
  • app:layout_constraintHorizontal_bias="0" 在两个视图上,这意味着当文本不够长到达屏幕边缘时,它会粘在锚视图的边缘
  • app:layout_constrainedWidth="true" 在两个视图上,这可以防止 textview 比其约束更宽,因此文本换行