【问题标题】:Issue with ConstraintLayout and TextView wrap_contentConstraintLayout 和 TextView wrap_content 的问题
【发布时间】:2016-09-28 09:03:55
【问题描述】:

自从我更新到 Android Studio 2.2 并开始使用 ConstraintLayout 和新的 UI Builder 后,出现了一个我无法解决的问题。

我有一个简单的布局,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)

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    只需使用类似于wrap_contentmatch_constraints(0dp)。意义

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

    这意味着您的布局将根据您设置的约束包装其内容。

    【讨论】:

    • 请为这个答案投票。换行,就像魅力一样。
    【解决方案2】:

    您使用的是哪个版本的 ConstraintLayout? alpha 8 有一个与此相关的回归。尝试更新到 alpha 9(如果您看到异常,请不要忘记重新启动工作室和/或使缓存无效),这应该可以解决您的问题。

    【讨论】:

    • 它不工作。我已经安装了 alpha 9 和 ConstraintLayout 1.0.2 但它不工作。内容超出屏幕
    【解决方案3】:

    尝试关注

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多