Как соблюдать размер представления внутри элемента RecyclerView - PullRequest
0 голосов
/ 07 августа 2020

У меня есть RecyclerView элемент с одним изображением и двумя текстовыми представлениями.

Если текст слишком длинный, это приводит к тому, что один элемент будет больше, чем другой, и в целом плохой интерфейс.

Что я могу сделать, чтобы ограничить текст с учетом ширины другого вида - изображения в этом случае? («Три точки» будут для меня правильным решением)

мой код макета для элемент просмотра recycler:

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

Изображение для пояснения проблемы -

мне бы

1 Ответ

0 голосов
/ 07 августа 2020

Ну, вы заставляете каждый элемент принимать требуемую ширину, используя android:layout_width="wrap_content" в каждом представлении. Если вы можете исправить ширину вашего ImageView, тогда, используя 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 в результате будет -

введите описание изображения здесь

Удачного кодирования!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...