首页 > 解决方案 > 如何尊重 RecyclerView 项目内的视图大小

问题描述

我有RecyclerView一个带有一个图像和两个文本视图的项目。

如果文本太长,则会导致一个项目会比另一个项目大,并且总体上是一个糟糕的 UI。

我可以做些什么来限制文本以尊重另一个视图的宽度 - 在这种情况下的图像?(“三个点”对我来说是正确的解决方案)

我的回收站视图项目的布局代码:

<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="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/cast_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/cast_name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="@+id/cast_image"
        app:layout_constraintStart_toStartOf="@+id/cast_image"
        app:layout_constraintTop_toBottomOf="@+id/cast_image" />

    <TextView
        android:id="@+id/cast_char_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="@+id/cast_name_text"
        app:layout_constraintStart_toStartOf="@+id/cast_name_text"
        app:layout_constraintTop_toBottomOf="@+id/cast_name_text" />
</androidx.constraintlayout.widget.ConstraintLayout>

用于澄清问题的图像 -

ID

标签: androidandroid-layoutandroid-recyclerviewandroid-view

解决方案


android:layout_width="wrap_content"好吧,您正在通过在每个视图中使用来强制每个项目采用所需的宽度。如果您可以修复 ImageView 的宽度,那么通过android:maxWidth="XXdp"在 Text 视图中使用,您可以实现您想要的。

代码如下 -

<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="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/cast_image"
        android:layout_width="100dp"                 //here fixing width to 100dp
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/cast_name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="100dp"                      //fixing TextView to max 100dp
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="@+id/cast_image"
        app:layout_constraintStart_toStartOf="@+id/cast_image"
        app:layout_constraintTop_toBottomOf="@+id/cast_image" />

    <TextView
        android:id="@+id/cast_char_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="@+id/cast_name_text"
        app:layout_constraintStart_toStartOf="@+id/cast_name_text"
        app:layout_constraintTop_toBottomOf="@+id/cast_name_text" />
</androidx.constraintlayout.widget.ConstraintLayout>

以下 xml 将导致 -

在此处输入图像描述

快乐编码!


推荐阅读