android - 如果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>
关于如何解决这个问题的任何帮助?
解决方案
- 从CircleImageView中删除 app:layout_constraintBottom_toBottomOf="parent"约束。
- 添加约束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>
推荐阅读
- python - 在 Python 中使用 OpenCv2 编写多个图像
- reactjs - React-native useContext 返回 undefined
- azure-functions - Azure Functions 核心工具 - 无法访问已处置的对象。对象名称:'IServiceProvider'
- git - 在现有 GitLab 项目上进行协作需要哪些步骤?
- asp.net-core - 当 Azure AD 和身份服务器通过 cookie 进行消息交换时不可用?
- php - Drupal 7,根据接口验证节点
- excel - 插入空白行的 VBA 代码并不总是有效
- python - 如何通过简单的文件读取加载 jsonlines 文件
- laravel - Laravel 从数据库中实时获取
- node.js - 如何在节点 azure iot sdk 中添加消息属性