如何让我的TextView自适应内容并限制最大宽度

6
我有一个包含三个视图 (ImageView、TextView、TextView) 的constraintLayout,如下所示:my constraintLayout layout 我希望绿色的TextViewwrap_content并且有一个最大宽度,我没有确切的最大宽度值,但我的TextView应该遵守 16dp 的最小边距。否则会出现多行显示。
请帮忙解决这个问题,谢谢!

请发布XML代码。 - Adil
1
你尝试过在android:layout_width:"wrap_content"app:layout_constrainedWidth="true"一起使用吗? - HawkPriest
3个回答

13
从版本1.1.0的ConstraintLayout开始,使用app:layout_constrainedWidth=”true”解决了此问题。

1
您可以将绿色的TextView放置在RelativeLayout中:
<android.support.constraint.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="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="Text1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@color/colorAccent"
            android:text="TEXT TEXT TEXT TEXT TEXT TEXT "/>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

它的行为如下所示:

Short text

short text

长文本

long text


1
我认为这不是你应该使用约束布局的方式。 - Abilash
尝试使用约束布局中可用的屏障。 - Abilash
使用约束布局的想法是避免嵌套视图,我们不应该这样做。 - display name

0

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="TextView TextView TextView TextView TextView TextViewTextView TextView TextView TextView TextView TextView "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接