ConstraintLayout 中的 TextView 剪辑文本和 layout_margin 无法正确显示

TextView inside ConstraintLayout clips text and layout_margin cant not be shown correctly

我使用: 编译 'com.android.support.constraint:constraint-layout:1.0.2'

主要问题是:

  1. layout_margin无法正确显示;
  2. 子文本视图的文本被剪裁。

详情如下:

这是我的 xml:

<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="wrap_content"
    android:background="@android:color/darker_gray"
    android:padding="8dp">

    <ImageView
        android:id="@+id/reviewer_avatar"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@color/colorAccent"
        android:contentDescription="@string/avatar"
        android:layout_margin="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/reviewer_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reviewer_name"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/comment_floor"
        app:layout_constraintLeft_toRightOf="@+id/reviewer_avatar"
        app:layout_constraintTop_toTopOf="@+id/reviewer_avatar"
        app:layout_constraintVertical_chainStyle="packed"/>

    <TextView
        android:id="@+id/comment_floor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reviewer_floor_text"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="@+id/reviewer_avatar"
        app:layout_constraintLeft_toRightOf="@+id/reviewer_avatar"
        app:layout_constraintTop_toBottomOf="@+id/reviewer_name"/>

    <TextView
        android:id="@+id/comment_period"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:text="@string/comment_period_text"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/comment_floor"
        app:layout_constraintTop_toBottomOf="@+id/reviewer_name"/>

    <TextView
        android:id="@+id/comment_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/large_text"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@+id/reviewer_avatar"
        app:layout_constraintTop_toBottomOf="@+id/reviewer_avatar"/>
</android.support.constraint.ConstraintLayout>

这是我的截图:

我想我找到了子文本视图的文本被剪裁的答案。

我改:

<TextView
    android:id="@+id/comment_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/large_text"
    android:textSize="16sp"
    app:layout_constraintLeft_toRightOf="@+id/reviewer_avatar"
    app:layout_constraintTop_toBottomOf="@+id/reviewer_avatar"/>

<TextView
        android:id="@+id/comment_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/large_text"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@+id/reviewer_avatar"
        app:layout_constraintTop_toBottomOf="@+id/reviewer_avatar"
        app:layout_constraintRight_toRightOf="parent"/>

这将解决问题![​​=13=]

但是这样TextView就不能缩放了,只能填充宽度。

最后,更好的答案:

  <TextView
        android:id="@+id/comment_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/reviewer_name"
        android:textSize="16sp"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintLeft_toLeftOf="@+id/reviewer_name"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reviewer_avatar"
        app:layout_constraintWidth_default="wrap"/>
  1. layout_margin 无法正确显示; - 边距 space 超出视图范围。 app:layout_constraintLeft_toRightOf="@+id/reviewer_avatar" 表示 reviewer_name 被限制在 reviewer_avatar 的右侧,不包括边距。设置 android:layout_marginLeft="8dp" of reviewer_name, comment_floor and comment_content.

  2. 子文本视图的文本被剪裁。 - comment_content 的宽度等于您的 ConstraintLayout 的宽度,但是 comment_content 被限制在 reviewer_avatar 的右侧。因此 comment_content 的右侧超出了 ConstraintLayout 的边界,即被剪裁。设置不同屏幕尺寸comment_content的具体宽度

我认为这是一个调整大小的错误,应该在 1.1 beta 1 中修复(请参阅 https://androidstudio.googleblog.com/2017/05/constraintlayout-110-beta-1-release.html 安装它)-- 请试试看是否是这样。

这里主要有两个问题

  1. 您未设置 right constraint 以便与父级一起查看
  2. ConstraintLayout 中存在一个 bug wrap_content 已解决, 现在你必须使用 match_constraint(0dp)layout_constraintWidth_default 属性 来解决这个问题 将以下两个属性添加到大文本视图
android:layout_width="0dp"
app:layout_constraintWidth_default="wrap"
app:layout_constraintRight_toRightOf="parent"

所以你的大文本视图会像

<TextView
        android:id="@+id/comment_content"
        android:layout_width="0dp"
        app:layout_constraintWidth_default="wrap"
        android:layout_height="wrap_content"
        android:text="@string/large_text"
        android:textSize="16sp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/reviewer_avatar"
        app:layout_constraintTop_toBottomOf="@+id/reviewer_avatar"/>