ConstraintLayout和TextView wrap_content的问题

3
自从我更新到Android Studio 2.2并开始使用新的UI Builder和ConstraintLayout之后,出现了一个问题,我无法解决。
我有一个简单的布局,其中包含ImageView和位于ImageView右侧的TextViewTextView中的文本会动态地从服务器更新(我不知道这个文本的确切长度)。 我想让TextView利用ImageView和屏幕右侧之间的所有可用空间。 以前,我使用RelativeLayout并设置TextView android:layout_width="wrap_content"即可解决此问题。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="116dp"
        android:layout_height="178dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        tools:src="@drawable/pets"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_toEndOf="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."/>
</RelativeLayout>

RelativeLayout(截图)

但是当我使用ConstraintLayout时,没有文字换行,TextView会超出屏幕。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="116dp"
        android:layout_height="178dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/pets"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:ellipsize="none"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."
        app:layout_constraintLeft_toRightOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

ConstraintLayout(屏幕截图)

3个回答

7
只需使用match_constraints(0dp),它类似于wrap_content。这意味着...
<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:ellipsize="none"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."
    app:layout_constraintLeft_toRightOf="@+id/imageView"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

这意味着您的布局将根据您设置的限制自动换行内容。

1
只需一行代码更改,就能完美运行。请为此回答点赞。 - Yao

0
你使用的ConstraintLayout版本是哪个?alpha 8版本存在相关问题。尝试更新到alpha 9版本(如果看到异常,请不要忘记重新启动studio和/或使缓存失效),这应该可以解决你的问题。

它不起作用。我已安装alpha 9和ConstraintLayout 1.0.2,但它仍然无法运行。内容超出了屏幕。 - Amit

-1

请尝试以下操作

<?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <ImageView
    android:id="@+id/imageView"
    android:layout_width="116dp"
    android:layout_height="178dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    tools:src="@drawable/pets"/>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_toEndOf="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie tincidunt mi a gravida. Aenean faucibus a sapien ut consequat. Praesent sed placerat quam."/>
    </RelativeLayout>
    </android.support.constraint.ConstraintLayout>

你不应该将ConstraintLayout与RelativeLayout一起使用,你可以查看这篇文章 https://dev59.com/aloU5IYBdhLWcg3wk3l2 - Eugene Brusov

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