2016-09-28 138 views
3

由于我已更新到Android Studio 2.2并开始使用ConstraintLayout与新的UI生成器有一个问题,我无法解决。ConstraintLayout和TextView的问题wrap_content

我有一个简单的布局,ImageViewTextViewImageView的右边。 TextView中的文本从服务器更新动态(我不知道这个文本的确切长度)。我想TextView使用ImageView和屏幕右侧之间的所有可用空间。与RelativeLayout早些时候我曾经 TextViewandroid: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 (ScreenShot)

但是当我使用ConstraintLayout,没有textwrap和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 (ScreenShot)

回答

1

您正在使用哪个ConstraintLayout的版本?阿尔法8有一个与此有关的回归。尝试更新为alpha 9(如果发现异常,请不要忘记重新启动studio和/或使缓存失效),这应该可以解决您的问题。

+0

是啊!有用!谢谢! – rbaloo

+0

它不工作。我已经安装了alpha 9&ConstraintLayout 1.0.2,但它不工作。内容超出屏幕 – Amit

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

你不应该使用ConstraintLayout与RelativeLayout结合,你可以检查这篇文章https://stackoverflow.com/questions/37321448/differences-between-constraintlayout-and-relativelayout –

2

只要去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

这是正确的答案。 – korrekorre