如何使用 ConstraintLayout 将两个垂直 TextView 一侧的 ImageView 居中

问题描述 投票:0回答:0

我需要一个 ImageView 位于 2 个垂直 TextView 右侧的布局,如下所示:

我希望 ImageView 和 TextView 都在父布局中居中,尽管 TextView 应该在一起。

我手头的xml布局代码是这个:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/image_view"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:background="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1"
        app:layout_constraintEnd_toStartOf="@+id/text_view_2"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/text_view_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:text="some text here that can be more than text view 1"
        app:layout_constraintBottom_toTopOf="@+id/text_view_2"
        app:layout_constraintEnd_toEndOf="@+id/text_view_2"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintStart_toStartOf="@+id/text_view_2"
        app:layout_constraintTop_toTopOf="@+id/image_view" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/text_view_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto_400"
        android:text="some here"
        android:textColor="@android:color/white"
        android:textSize="40sp"
        app:layout_constraintBottom_toBottomOf="@+id/image_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/image_view"
        app:layout_constraintTop_toBottomOf="@+id/text_view_1"  />


</androidx.constraintlayout.widget.ConstraintLayout>

我只知道如何用其中一个 TextView 来约束 ImageView。 当来自 text_view_1 的文本太大时,它会从屏幕上掉下来。如果我把

layout_constrainedWidth
放在
text_view_1
中,它将被限制为
text_view_2
的宽度。

在这种情况下如何设置约束,以便每个 TextView 可以增加其大小直到达到可用空间并折叠到第二行?

我已经通过将 TextViews 放在 LinearLayout 中完成了所需的行为,但我想知道如何避免额外的布局。

编辑:

我想要的更多图片:

我想避免这种情况:

android android-constraintlayout
© www.soinside.com 2019 - 2024. All rights reserved.