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

标签 android android-constraintlayout

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

主要问题是:

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

详情如下:

这是我的 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>

这是我的截图:

This is my screenshot

最佳答案

这里主要是两个问题

  1. 您没有设置正确约束以便与 parent 一起查看
  2. ConstraintLayout 中的bug wrap_content 已解决,现在您必须使用 ma​​tch_constraint(0dp)layout_constraintWidth_default 属性来解决这个问题 将以下两个属性添加到大 TextView
android:layout_width="0dp"
app:layout_constraintWidth_default="wrap"
app:layout_constraintRight_toRightOf="parent"

所以你的大 TextView 会像

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

关于android - ConstraintLayout 中的 TextView 剪辑文本和 layout_margin 无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564330/

相关文章:

android - 从另一个 AsyncTask 调用 AsyncTask

android - 拍照后返回 fragment

android - 为 Horizo​​ntalScrollView 设置约束

android - 将最大 View 的左侧与约束布局对齐

android - 约束布局与嵌套 ScrollView 中的父级不匹配

android - 在 bazel 构建中使用 Android 约束布局

android - Intent 启动 Activity 后会调用哪个方法?

javascript - 了解英特尔 XDK 中的图片处理

android - 如何根据 ScrollView 中的项目设置 ListView 高度

android - 如何在 ConstraintLayout 中为 subview 赋予重力