1

Имеется следующий layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#EEEEEE"
    >

    <ImageView
        android:layout_width="4dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        />

    <TextView
        android:text="12345678911131517192123252729313335373941434547495153555759616365676971737577798183858789"
        android:id="@+id/title"
        android:textSize="15sp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="8dp"
        />

    <TextView
        android:text="  text text"
        android:id="@+id/prev"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/title"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="WHY WHY WHY"
        app:layout_constraintTop_toBottomOf="@+id/prev"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        />


</android.support.constraint.ConstraintLayout>

Получаем следующий результат:

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

Причем, если текст первого TextView изменить, например, на: "1234567891113151719212325272931333537394143454749515355575961636567697173757779818385878991" (т.е. добавить два символа), то последний TextView перестает наезжать на предпоследний. Также если у последнего TextView изменить layout_marginBottom на 0dp (а текст оставить как в примере), то проблема также исчезает. Вопрос: в чем проблема? и как исправить?

UPDATE:

Добавил полоску слева, которая имеет высоту match_parent. Из-за этого нельзя использовать paddingBottom в ConstraintLayout. Элемент используется в RecyclerView, поэтому нижнему элементу нужен margin.

2
  • Попробуйте в таком случае использовать привязку всего ко всему (верхний элемент к нижнему в т.ч.)
    – abbath0767
    28 авг 2017 в 9:14
  • В таком случае эти элементы будут в связке (chain), а я наоборот уходил от этого, т.к. там проявляется следующая бага, если использовать данный layout в RecyclerView: когда он переиспользует ViewHolder для элемента, который имеет меньшую высоту, то ConstraintLayout решает не менять высоту, а просто центрирует все элементы (если установлен app:layout_constraintVertical_chainStyle="packed"). 28 авг 2017 в 9:27

1 ответ 1

1

Была такая проблема пару раз, если нижний элемент привязан к низу родителя, похоже на баг ConstraintLayout, уберите у последнего Textview

app:layout_constraintBottom_toBottomOf="parent"

Или используйте chain, если привязка нужна

4
  • Дико извиняюсь, это я на сайте правил уже id, и забыл их везде отредактировать. 27 авг 2017 в 10:54
  • Потому что все зависит от размера экрана. Чтобы воспроизвести проблему на вашем экране, необходимо в первом TextView оставить столько символов, чтобы на третьей строчке их было 3. 27 авг 2017 в 11:51
  • @AlexanderErmakov, почему мое решение вам не подошло?
    – katso
    29 авг 2017 в 5:05
  • Я не могу убрать app:layout_constraintBottom_toBottomOf="parent", так как перестает работать margin нижнего элемента. Использование связок (chain) приводит к другому багу в RecyclerView. 29 авг 2017 в 6:16

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.