【发布时间】:2022-01-25 06:30:24
【问题描述】:
我有以 ConstraintLayout 作为父级的布局。在我的约束布局中有三个视图:ImageView 和两个 TextView。我需要,根据 ImageView 和 TextView (1) 的可见性是否可见,第二个 TextView 的边距顶部会发生变化。这是我想要实现的视觉示例:
我尝试将goneMarginTop = 32 用于我的第二个TextView。但是,在这种情况下,一旦第一个 TextView 的可见性消失,我就会在 ImageView 和 TextView = 32 dp 之间获得边距,但预期为 12 dp。仅当 ImageView 和第一个 TextView 消失时,我才需要 margin = 32 dp。这是我的代码:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivImage"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="14dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ivImage"
app:layout_goneMarginTop="32dp" />
<TextView
app:layout_goneMarginTop="32dp"
android:id="@+id/tvDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="14dp"
android:textColor="@color/black"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
是否可以使用布局中的 ConstraintLayout 来实现图像中的行为(不是以编程方式)。
请帮帮我。
【问题讨论】:
标签: android android-layout textview margin android-constraintlayout