首页 > 解决方案 > 如果textview太长,布局就毁了

问题描述

我有一个RecyclerView用于显示帖子的评论。如果文本很短,则布局很好。但如果太长,该项目上方会有一个空白区域。

这是我现在用于显示项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:foreground="?android:attr/selectableItemBackground">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/comments_bottom_sheet_list_item_user_image"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/comments_bottom_sheet_list_item_user_username"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:fontFamily="@font/raleway_bold"
    android:textColor="@android:color/black"
    app:layout_constraintEnd_toStartOf="@+id/comments_bottom_sheet_list_item_comment_time_ago"
    app:layout_constraintStart_toEndOf="@+id/comments_bottom_sheet_list_item_user_image"
    app:layout_constraintTop_toTopOf="@+id/comments_bottom_sheet_list_item_user_image"
    tools:text="username" />

<TextView
    android:id="@+id/comments_bottom_sheet_list_item_comment_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:fontFamily="@font/raleway_medium"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/comments_bottom_sheet_list_item_user_username"
    app:layout_constraintTop_toBottomOf="@+id/comments_bottom_sheet_list_item_user_username"
    tools:text="comment" />

<TextView
    android:id="@+id/comments_bottom_sheet_list_item_comment_time_ago"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:fontFamily="@font/raleway_medium"
    android:textSize="10sp"
    app:layout_constraintBottom_toBottomOf="@+id/comments_bottom_sheet_list_item_user_username"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/comments_bottom_sheet_list_item_user_username"
    tools:text="1 week ago" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是一个短文本的图片,一个长文本的图片。

关于如何解决这个问题的任何帮助?

标签: androidandroid-recyclerviewtextview

解决方案


  1. 从CircleImageView中删除 app:layout_constraintBottom_toBottomOf="parent"约束。
  2. 添加约束app:layout_constraintBottom_toTopOf用户名部分。

尝试

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:foreground="?android:attr/selectableItemBackground">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/comments_bottom_sheet_list_item_user_image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/comments_bottom_sheet_list_item_user_username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:textColor="@android:color/black"
        app:layout_constraintEnd_toStartOf="@+id/comments_bottom_sheet_list_item_comment_time_ago"
        app:layout_constraintStart_toEndOf="@+id/comments_bottom_sheet_list_item_user_image"
        app:layout_constraintTop_toTopOf="@+id/comments_bottom_sheet_list_item_user_image"
        app:layout_constraintBottom_toTopOf="@+id/comments_bottom_sheet_list_item_comment_text"
        tools:text="username" />

    <TextView
        android:id="@+id/comments_bottom_sheet_list_item_comment_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/comments_bottom_sheet_list_item_user_username"
        app:layout_constraintTop_toBottomOf="@+id/comments_bottom_sheet_list_item_user_username"
        tools:text="comment" />

    <TextView
        android:id="@+id/comments_bottom_sheet_list_item_comment_time_ago"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:textSize="10sp"
        app:layout_constraintBottom_toBottomOf="@+id/comments_bottom_sheet_list_item_user_username"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/comments_bottom_sheet_list_item_user_username"
        tools:text="1 week ago" />

</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读