我想在第一個視圖的底部應用邊距。
但是,我發現bottom margin上述觀點并不適用。
但是當我給top margin下面的視圖時,它作業正常。
為什么是這樣?
這是一個示例圖片和代碼。
當底部邊距應用于上述(測驗)視圖時

<androidx.constraintlayout.widget.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="match_parent">
<TextView
android:id="@ id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textSize="24sp"
android:background="@color/light_blue_400"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="30dp"
app:layout_constraintBottom_toTopOf="@id/test2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray_400"
android:text="TEST2"
android:textSize="24sp"
android:layout_marginLeft="10dp"
app:layout_constraintTop_toBottomOf="@id/test"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
當上邊距應用于下面(test2)視圖時

<androidx.constraintlayout.widget.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="match_parent">
<TextView
android:id="@ id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textSize="24sp"
android:background="@color/light_blue_400"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
app:layout_constraintBottom_toTopOf="@id/test2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray_400"
android:text="TEST2"
android:textSize="24sp"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
app:layout_constraintTop_toBottomOf="@id/test"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
首先,僅當您在同一方向設定約束時才應用邊距,因此在第二個代碼android:layout_marginTop中應用了layout_constraintTop_toBottomOf.
但是在您添加的第一個代碼中layout_marginBottom并且layout_constraintBottom_toTopOf沒有作業,她的原因是什么?
test2依賴于,test因為它在相反方向(即底部)上沒有約束。

并且由于添加app:layout_constraintBottom_toTopOf="@id/test2"到test,它根本沒有做任何事情,我確定您對其進行了硬編碼,但是如果您嘗試使用設計面板執行此操作,它將不允許您執行此操作,這是就像您創建非完整鏈一樣。
試試這個代碼
<androidx.constraintlayout.widget.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="match_parent">
<TextView
android:id="@ id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="50dp"
android:background="@color/light_blue_400"
android:text="TEST"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/test2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="30dp"
android:background="@color/gray_400"
android:text="TEST2"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/test" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后嘗試TextViews在設計面板中移動 ,您會注意到您無法TextViews向上或向下移動,這是因為我在和之間創建了一個Vertical-Chine。testtest2

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/495848.html
