如果 textview 太长,布局就会被破坏
If textview is too long, the layout becomes ruined
我有一个 RecyclerView
用于显示 post 的评论。如果文本很短,布局就很好。但是如果太长,那一项上面就会有一个空白space。
这是我现在用于显示项目的布局:
<?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>
并且 this is a picture of a short text and this 用于长文本。
关于如何解决这个问题的任何帮助?
- 从 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>
如果你想使用简单的,试试这个LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal">
<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_gravity="top"
android:layout_marginBottom="16dp"
tools:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/comments_bottom_sheet_list_item_user_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="16dp"
android:fontFamily="@font/raleway_bold"
android:textColor="@android:color/black"
tools:text="username" />
<TextView
android:id="@+id/comments_bottom_sheet_list_item_comment_time_ago"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:fontFamily="@font/raleway_bold"
tools:text="1 week ago" />
</LinearLayout>
<TextView
android:id="@+id/comments_bottom_sheet_list_item_comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="@font/raleway_bold"
tools:text="comment" />
</LinearLayout>
</LinearLayout>
我有一个 RecyclerView
用于显示 post 的评论。如果文本很短,布局就很好。但是如果太长,那一项上面就会有一个空白space。
这是我现在用于显示项目的布局:
<?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>
并且 this is a picture of a short text and this 用于长文本。
关于如何解决这个问题的任何帮助?
- 从 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>
如果你想使用简单的,试试这个LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal">
<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_gravity="top"
android:layout_marginBottom="16dp"
tools:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/comments_bottom_sheet_list_item_user_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="16dp"
android:fontFamily="@font/raleway_bold"
android:textColor="@android:color/black"
tools:text="username" />
<TextView
android:id="@+id/comments_bottom_sheet_list_item_comment_time_ago"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:fontFamily="@font/raleway_bold"
tools:text="1 week ago" />
</LinearLayout>
<TextView
android:id="@+id/comments_bottom_sheet_list_item_comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="@font/raleway_bold"
tools:text="comment" />
</LinearLayout>
</LinearLayout>