Добавление отступа в верхнюю часть CardView приводит к увеличению длины текста в TextView. - PullRequest
1 голос
/ 04 ноября 2019

У меня есть TextView внутри ConstraintLayout (CL), который находится внутри CardView (CV). CL является единственным элементом внутри CV, а внутри CL есть несколько разных ImageViews и TextViews.

Текст и изображения заполняются программно через RecyclerViewAdapter и ViewHolder. Когда текст в последнем TextView становится достаточно длинным, в верхней части представления карты начинает появляться отступ.

Макет xml с тестовой строкой:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/viewStopScheduleItem_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="1dp"
    android:clickable="true"
    android:focusable="true"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="0dp"
    app:cardElevation="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/viewStopScheduleItem_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <ImageView
            android:id="@+id/viewStopScheduleItem_barImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/status_bar_item_background"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp" />

        <ImageView
            android:id="@+id/viewStopScheduleItem_busImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_directions_bus_white"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginBottom="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeShortNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginTop="0dp"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginLeft="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginTop="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_stopTime1TextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM"
            android:textAlignment="viewStart"
            android:layout_gravity="start"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Чем длиннее становится строка, тем больше появляется отступов.

Что я пробовал:

  • Установка отступов и полей CV и CL на ноль,
  • Возиться свысота всех элементов и чем меньше высота текстового представления, тем меньше становится отступ, но тогда полная строка не видна.
  • Установка высоты представлений и макетов равными match_parent и wrap_content безуспешно.
  • Установка силы тяжести CV и CL для начала и начала.

Итак, как мне избавиться от отступов?

Я прикрепил скриншоты

Длинная строка, вызывающая заполнение

Long string causing padding

Короткая строка без подкладки

Short String without padding

Ответы [ 2 ]

1 голос
/ 05 ноября 2019

@ TheHeuman, MohammadRexa Ответ Абири решит вашу проблему, но, похоже, вы могли бы очистить свой XML-файл больше. Пожалуйста, посмотрите ниже XML и прикрепленный скриншот.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView                  
  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:id="@+id/viewStopScheduleItem_cardView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="1dp"
  android:clickable="true"
  android:focusable="true"
  app:cardBackgroundColor="@android:color/white"
  app:cardCornerRadius="0dp"
  app:cardElevation="4dp">

  <androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/viewStopScheduleItem_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <ImageView
      android:id="@+id/viewStopScheduleItem_barImageView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:srcCompat="@android:drawable/status_bar_item_background" />

    <ImageView
      android:id="@+id/viewStopScheduleItem_busImageView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      app:layout_constraintStart_toEndOf="@+id/viewStopScheduleItem_barImageView"
      app:layout_constraintTop_toTopOf="parent"
      tools:background="@android:color/black"
      tools:layout_height="48dp"
      tools:layout_width="48dp" />

    <TextView
      android:id="@+id/viewStopScheduleItem_routeShortNameTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_routeLongNameTextView"
      app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_routeLongNameTextView"
      tools:text="TextView" />

    <TextView
      android:id="@+id/viewStopScheduleItem_routeLongNameTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      app:layout_constraintStart_toEndOf="@+id/viewStopScheduleItem_busImageView"
      app:layout_constraintTop_toTopOf="parent"
      tools:text="TextView" />

    <TextView
      android:id="@+id/viewStopScheduleItem_stopTime1TextView"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      android:gravity="start"
      android:layout_marginTop="8dp"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toEndOf="@+id/viewStopScheduleItem_busImageView"
      app:layout_constraintTop_toBottomOf="@+id/viewStopScheduleItem_routeLongNameTextView"
      tools:text="1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM" />

  </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

enter image description here

1 голос
/ 04 ноября 2019

, если вы хотите удалить padding_top, вам нужно изменить layout_constraint следующим образом:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/viewStopScheduleItem_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="1dp"
    android:clickable="true"
    android:focusable="true"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="0dp"
    app:cardElevation="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/viewStopScheduleItem_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <ImageView
            android:id="@+id/viewStopScheduleItem_barImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/status_bar_item_background"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp" />

        <ImageView
            android:id="@+id/viewStopScheduleItem_busImageView"
            android:layout_width="32dp"
            android:layout_height="32dp"
            app:srcCompat="@drawable/status_bar_item_background"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/viewStopScheduleItem_barImageView"
            android:layout_marginBottom="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeShortNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintTop_toTopOf="parent" \\
            android:layout_marginTop="8dp"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginLeft="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="parent" \\
            android:layout_marginTop="8dp" />

        <TextView
            android:id="@+id/viewStopScheduleItem_stopTime1TextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:
            29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:
            29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1
            :29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:
            9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29
             PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29
             PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:9 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM
             1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM 1:29 PM"
            android:textAlignment="viewStart"
            android:layout_gravity="start"
            app:layout_constraintLeft_toRightOf="@+id/viewStopScheduleItem_busImageView"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/viewStopScheduleItem_routeLongNameTextView"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
...