在ConstraintLayout中引用的视图消失时保留边距

时间:2017-10-03 16:42:35

标签: android android-layout

我有2个TextView,如下面的xml所示。 如果我在运行时隐藏textView2,我会失去底部边距。 当textView消失时,如何将textView2和父级之间的下限保持为16dp。

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

  <TextView
      android:id="@+id/textView"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="16dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:text="abc"
      app:layout_constraintBottom_toTopOf="@+id/textView2"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_chainStyle="packed"
      />

  <TextView
      android:id="@+id/textView2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="32dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:text="xyz"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/textView"
      app:layout_constraintVertical_chainStyle="packed"/>
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:31)

使用layout_goneMarginBottom

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

  <TextView
      android:id="@+id/textView"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="16dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:text="abc"
      app:layout_goneMarginBottom="16dp"
      app:layout_constraintBottom_toTopOf="@+id/textView2"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_chainStyle="packed"
      />

  <TextView
      android:id="@+id/textView2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="32dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:text="xyz"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/textView"
      app:layout_constraintVertical_chainStyle="packed"/>
</android.support.constraint.ConstraintLayout>