Если textview слишком длинный, макет становится разрушенным - PullRequest
2 голосов
/ 20 апреля 2020

У меня есть 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>

И это представляет собой изображение короткого текста, а это для длинного текста.

Любая помощь в том, как решить эту проблему?

Ответы [ 2 ]

1 голос
/ 20 апреля 2020

Попробуйте, если хотите использовать простые 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>
1 голос
/ 20 апреля 2020
  1. Удалить app: layout_constraintBottom_toBottomOf = "parent" Ограничение из CircleImageView .
  2. Добавить ограничение 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...