ConstraintLayout除了可变宽度的TextView之外,始终具有ImageView

问题描述 投票:1回答:2

我无法解决这个看似简单的布局问题:

我有一个TextView,我想有一个带图标的ImageView。我不能使用CompoundDrawable,因为该图标需要可点击。另外TextView也必须为wrap_content,因为它具有背景。

设置为TextView的文本的长度是可变的。如果文本较短,则TextView应该仅占用其所需的空间,并且图标应在其旁边。如果文本较长,则图标应在最右边,TextView占据所有空格。

所以这就是我想要的:

enter image description here

我尝试过的:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0000ff"
        android:text="short text"
        android:textColor="#ffffff"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/tv"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_close_black_24dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

这适用于短文本,但是当文本较长时,TextView会占据所有空间,并在屏幕外“推动”图标。

如何解决?

android android-layout android-constraintlayout
2个回答
0
投票

这将起作用

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

    <TextView
        android:id="@+id/textView57"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/textView57"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_add_black_24dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

0
投票

要解决此问题,您可以将垂直方向的指导方针用作

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/long_text"
        app:layout_constraintRight_toLeftOf="@id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/guideline"
        app:layout_constraintTop_toTopOf="@id/tv"
        app:layout_constraintBottom_toBottomOf="@id/tv"
        android:src="@drawable/ic_launcher_background" 
        app:layout_constraintRight_toRightOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.7"/>

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

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