Поля ConstraintLayout не работают

Использование xml ниже marginTop игнорируется, если видимость представления, которой я ограничен, исчезла.

Это происходит с последней версией библиотеки макетов на данный момент com.android.support.constraint:constraint-layout:1.0.0-beta4

Пример:

tvMessage и ivCommentImage равны visible - верхнее поле 16dp на llLeftActions и llRightActions работает нормально. Если ivCommentImage равно gone, поле игнорируется.

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp">

<!-- some more views here -->

    <TextView
        android:id="@+id/tvMessage"
        style="@style/SocialFeed.Description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivProfile"
        app:layout_goneMarginTop="0dp"
        tools:text="@string/lorem_ipsum_140chars"/>

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/ivCommentImage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvMessage"
        app:layout_goneMarginTop="0dp"
        />

    <android.support.constraint.Guideline
        android:id="@+id/gCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>

    <LinearLayout
        android:id="@+id/llLeftActions"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="center_vertical|left"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="@+id/tvMessage"
        app:layout_constraintRight_toLeftOf="@+id/gCenter"
        app:layout_constraintTop_toBottomOf="@+id/ivCommentImage"
        app:layout_goneMarginTop="0dp"
        />

    <LinearLayout
        android:id="@+id/llRightActions"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="center_vertical|right"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="@+id/gCenter"
        app:layout_constraintRight_toRightOf="@+id/tvMessage"
        app:layout_constraintTop_toBottomOf="@+id/ivCommentImage"
        app:layout_goneMarginTop="0dp"/>


</android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

person Ognian Gloushkov    schedule 29.11.2016    source источник


Ответы (2)


Он работает полностью так, как задумано — обратите внимание, что вы добавили layout_goneMarginTop="0dp" к @id/llLeftActions и @id/llRightActions...

Это означает, что когда виджет, на который они указывают (@id/ivCommentImage), помечен как исчезнувший, поле, которое будет использоваться для верхнего соединения, будет... 0dp, а не 16dp. Вот для чего нужны эти атрибуты! :)

ознакомьтесь с документацией по поведению видимости и атрибуты исчезнувших полей

person Nicolas Roard    schedule 30.11.2016

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Available chats"
    tools:layout_editor_absoluteX="1dp"
    tools:layout_editor_absoluteY="1dp" />

<ListView
    android:id="@+id/listChats"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/textView"/>

Обратите внимание на последнюю строку: добавление ограничений по краям заставляет ограничение работать.

Вы также можете использовать представление дизайна в Studio и перетаскивать ограничения между объектами.

person Mindborg    schedule 06.07.2017
comment
у меня такая же проблема, @+id/textView работает, а @id/textView не работает. почему @+id/textView не просто @id/textView - person M Moersalin; 05.06.2018