gpt4 book ai didi

android - ConstraintLayout 和 TextView wrap_content 问题

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:20 24 4
gpt4 key购买 nike

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

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

最佳答案

只需使用类似于 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"/>

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

关于android - ConstraintLayout 和 TextView wrap_content 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39742728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com