【问题标题】:How to respect view size inside RecyclerView item如何尊重 RecyclerView 项目内的视图大小
【发布时间】:2020-08-07 10:12:57
【问题描述】:

我有 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>

用于澄清问题的图片 -

【问题讨论】:

    标签: android android-layout android-recyclerview android-view


    【解决方案1】:

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

    代码如下-

    <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 将导致 -

    编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多