我正在尝试使用 ConstraintLayout
实现一个简单的聊天气泡。这就是我想要实现的目标:
但是,wrap_content
并没有做我想做的事。它尊重边距,但会扩展到 View 边界之外。这是我的布局:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0"
tools:background="@drawable/chat_message_bubble"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sodales accumsan tortor at bibendum."
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp" />
</android.support.constraint.ConstraintLayout>
呈现如下:
我正在使用 com.android.support.constraint:constraint-layout:1.0.0-beta4
。
我做错了吗?这是一个错误还是只是一个不直观的行为?我可以使用 ConstraintLayout
实现正确的行为吗(我知道我可以使用其他布局,我特别询问 ConstrainLayout
)。
最佳答案
更新(ConstraintLayout 1.1.+)
使用 app:layout_constrainedWidth="true"
和 android:layout_width="wrap_content"
以前(已弃用):
app:layout_constraintWidth_default="wrap"
与 android:layout_width="0dp"
关于android - ConstraintLayout 内的 Wrap_content View 延伸到屏幕外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850966/