How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

huangapple go评论51阅读模式
英文:

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

问题

以下是要翻译的内容:

问题

如何在ConstraintLayout中居中显示一个ImageView和一个TextView,确保它们保持居中,而不管ConstraintLayout的高度如何,同时还确保ImageView在必要时缩小以保持TextView的可见性?

预期结果:

对于大型ConstraintLayout(居中,但不大于原始图像):

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

对于小型ConstraintLayout(居中,缩小的图像,文本仍然可见):

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

我创建了以下可复现的示例:

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="500dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:srcCompat="@tools:sample/avatars"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/textView" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingHorizontal="@dimen/primary_margin"
            android:text="Just a text"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView"
            app:layout_constraintBottom_toBottomOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

编辑

到目前为止,我找到的最接近的解决方案如下。然而,唯一剩下的缺点是我需要手动指定最大宽度和高度,而我的理想情况是根据图像的实际宽度和高度动态调整它们。

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="16dp"
            app:layout_constraintHeight_max="200dp"
            app:layout_constraintWidth_max="200dp"
            tools:srcCompat="@tools:sample/avatars"
            app:layout_constraintVertical_chainStyle="packed"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/textView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingHorizontal="@dimen/primary_margin"
            android:text="Just a text"
            app:layout_constraintTop_toBottomOf="@id/imageView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
英文:

Question

How can I center an ImageView and a TextView within a ConstraintLayout, ensuring that they remain centered regardless of the ConstraintLayout's height, while also ensuring that the ImageView shrinks if necessary to maintain visibility of the TextView?

Expected results:

In case of a large ConstraintLayout (centered, but not larger than the original image):

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

In case of a small ConstraintLayout (centered, shrinked image, text still visible):

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

I build the following reproducible example:

&lt;androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;500dp&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/imageView&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            tools:srcCompat=&quot;@tools:sample/avatars&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            app:layout_constraintBottom_toTopOf=&quot;@id/textView&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/textView&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:paddingHorizontal=&quot;@dimen/primary_margin&quot;
            android:text=&quot;Just a text&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintTop_toBottomOf=&quot;@+id/imageView&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot; /&gt;

    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Edit

The closest solution I have found so far is as follows. However, the only remaining drawback is that I need to manually specify the maximum width and height, whereas my ideal scenario would be to dynamically adjust them according to the image's actual width and height.

&lt;androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/imageView&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;0dp&quot;
            android:layout_marginBottom=&quot;16dp&quot;
            app:layout_constraintHeight_max=&quot;200dp&quot;
            app:layout_constraintWidth_max=&quot;200dp&quot;
            tools:srcCompat=&quot;@tools:sample/avatars&quot;
            app:layout_constraintVertical_chainStyle=&quot;packed&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            app:layout_constraintBottom_toTopOf=&quot;@id/textView&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/textView&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:paddingHorizontal=&quot;@dimen/primary_margin&quot;
            android:text=&quot;Just a text&quot;
            app:layout_constraintTop_toBottomOf=&quot;@id/imageView&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot; /&gt;

    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

如果您采用最接近的解决方案,请尝试将ImageView的高度和宽度再次设置为wrap_content(以便以其原生大小显示),但添加app:layout_constrainedWidth="true"app:layout_constrainedHeight="true"。这基本上让您定义所需的高度和宽度(即实际图像大小),但在必要时也会强制执行约束,因此它在必要时就像0dp(即MATCH_CONSTRAINT)一样运行:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constrainedHeight="true"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/text"
        app:layout_constraintVertical_chainStyle="packed"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi"
        android:textSize="85sp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/image"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

以下是在PhoneWear OS Square参考设备模板中的相同布局:

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

实际的ImageView被扭曲了(因为两个维度都尝试大小为wrap_content),但图像保持其纵横比以适应两个维度(如果您使用默认的scaleType)。

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

英文:

If you take your closest solution, try setting the ImageView height and width to wrap_content again (so it displays at its native size) but add app:layout_constrainedWidth=&quot;true&quot; and app:layout_constrainedHeight=&quot;true&quot;. This basically lets you define the height and width you want (i.e. whatever the image size actually is), but also enforces the constraints, so it acts like 0dp (i.e. MATCH_CONSTRAINT) when necessary:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:padding=&quot;16dp&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/image&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constrainedWidth=&quot;true&quot;
        app:layout_constrainedHeight=&quot;true&quot;
        android:src=&quot;@mipmap/ic_launcher_round&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintBottom_toTopOf=&quot;@id/text&quot;
        app:layout_constraintVertical_chainStyle=&quot;packed&quot;
        /&gt;

    &lt;TextView
        android:id=&quot;@+id/text&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Hi&quot;
        android:textSize=&quot;85sp&quot;
        android:layout_marginTop=&quot;16dp&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/image&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Here's the same layout in the Phone and Wear OS Square reference device templates:

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

The actual ImageView is distorted (because both dimensions are trying to size to wrap_content) but the image maintains its aspect ratio to fit in both dimensions (if you're using the default scaleType).

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

答案2

得分: 0

你需要使用紧凑型垂直链。你可以在这个视频链接中了解更多关于链的信息。以下是如何居中图像和文本的代码片段。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="500dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        app:layout_constraintStart_toStartOf="@id/imageView"
        app:layout_constraintEnd_toEndOf="@id/imageView"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

</androidx.constraintlayout.widget.ConstraintLayout>

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

英文:

You need to use packed vertical chaining. You can see more about chains in this video Link. Here is the code snippet how to center image and text.

&lt;androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;500dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;


        &lt;ImageView
            android:id=&quot;@+id/imageView&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            app:layout_constraintBottom_toTopOf=&quot;@+id/textView&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintHorizontal_bias=&quot;0.5&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            app:layout_constraintVertical_chainStyle=&quot;packed&quot;
            tools:srcCompat=&quot;@tools:sample/avatars&quot; /&gt;

        &lt;TextView
            app:layout_constraintStart_toStartOf=&quot;@id/imageView&quot;
            app:layout_constraintEnd_toEndOf=&quot;@id/imageView&quot;
            android:id=&quot;@+id/textView&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;TextView&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            app:layout_constraintTop_toBottomOf=&quot;@+id/imageView&quot; /&gt;

    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

How can I center an ImageView and TextView in a ConstraintLayout while adjusting ImageView size to maintain TextView visibility?

huangapple
  • 本文由 发表于 2023年6月1日 17:55:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定