【问题标题】:ConstraintLayout always have ImageView besides TextView of variable width除了可变宽度的 TextView 之外,ConstraintLayout 总是有 ImageView
【发布时间】:2020-04-06 11:12:20
【问题描述】:

这个看似简单的布局问题我解决不了:

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

设置为TextView 的文本长度可变。如果文本很短,TextView 应该只占用它需要的空间,并且图标应该就在它旁边。如果文本很长,图标应该在最右边,TextView 占据它左边的所有空间。

这就是我想要的:

我尝试过的:

<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


【解决方案1】:

这会起作用
[更新:- 使用横向线性布局]

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView57"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="1.8"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView20"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="0.2"
            app:srcCompat="@drawable/filled_compass" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 当文本视图使用 layout_width="wrap_content 时,这会将图像推到宽度之外
  • 谢谢!我也玩过重量,但不知何故错过了这个解决方案:)
  • @asco 如果您想要一个带有权重的解决方案,那么您应该在您的帖子中提到这一点,并且作为一名经验丰富的 SO 成员,请继续这样做。
  • @Pavneet_Singh 我不想要一个专门用于权重的解决方案,而是一个符合我描述的解决方案。
【解决方案2】:

要解决这个问题,您可以使用垂直方向的指南

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 2016-04-01
    • 2020-03-25
    • 2017-12-03
    • 1970-01-01
    • 2021-08-07
    • 2020-09-01
    • 2016-10-31
    相关资源
    最近更新 更多