0

嘿,我正在研究 android 中的约束布局。我正在尝试将我的文本视图和图像视图放在同一行中,并且我的图像尺寸更大。

在此处输入图像描述.

问题是我想给出适当的约束。设置适当的约束后,它将如下所示。

在此处输入图像描述

活动.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="10dp"
    android:paddingTop="12dp"
    tools:ignore="RtlSymmetry">

    <TextView
        android:id="@+id/testName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="blah blah blah" />

    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/testIcon"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/testName"
        tools:text="blah blah blah blah blah blah blah blah blah blah blah blah" />

    <ImageView
        android:id="@+id/testIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_pills"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/description"
        app:layout_constraintTop_toBottomOf="@+id/testName"
        tools:ignore="ContentDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>

有人可以指导我如何设置适当的约束。

4

1 回答 1

0

在第一个文本视图中,您需要进行以下更改:

android:layout_height="0dp"
app:layout_constraintEnd_toStartOf="@+id/testIcon"

在第二个文本视图中,您需要进行以下更改:

android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/testIcon"

在图像视图中,您需要删除此行:

app:layout_constraintStart_toEndOf="@+id/description"
app:layout_constraintTop_toBottomOf="@+id/testName"

这是我的示例工作代码:

<?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="wrap_content"
android:paddingHorizontal="@dimen/dimen_12"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toStartOf="@id/imageview"
    app:layout_constraintBottom_toTopOf="@id/textView1"
    android:text="blah blah blah"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/textView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@id/imageview"
    app:layout_constraintBottom_toBottomOf="@id/imageview"
    android:text="blah blah blah blah blah blah blah blah blah blah blah blah"/>

<ImageView
    android:id="@+id/imageview"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
于 2022-02-03T04:56:18.253 回答