android - ConstraintLayout 内的 Wrap_content View 延伸到屏幕外

标签 android android-layout android-constraintlayout

我正在尝试使用 ConstraintLayout 实现一个简单的聊天气泡。这就是我想要实现的目标:

enter image description here enter image description here

但是,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>

呈现如下:

enter image description here

我正在使用 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/

相关文章:

android - ConstraintLayout 内的 RecyclerView 项目不占据其父级的整个宽度

android - Jetpack compose 中的 ConstraintLayout 问题

java - 在设备启动时启动应用程序时出错

android - 我们可以使用带有设置首选项的 Android Jetpack 导航组件吗?

android - 如何在android中隐藏工具栏?

java - 具有顶部和底部边距的自定义按钮

java - 当循环到另一个列表java时尝试将元素添加到列表中

java - 如何保持微调器中选定的项目处于选中状态,直到用户选择不同的项目..?

android - 如何从编辑文本中删除边距

android - Travis CI 构建不适用于 Android 约束布局