我有以 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 來實作影像中的行為(不是以編程方式)。
請幫我。
uj5u.com熱心網友回復:
您可以以編程方式進行:
val layoutParams = imageView.layoutParams as RelativeLayout.LayoutParams
layoutParams.setMargins(100, 100, 100, 100)
uj5u.com熱心網友回復:
我認為沒有一種方法可以僅使用 XML 和gone邊距來滿足您的要求。您可以按照建議以編程方式執行此操作,但您也可以使用
電視標題 gone

ImageView 和 tvTitle gone

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422502.html
標籤:
