2016-09-28 137 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