約束布局中的這兩種情況是否等效?
第一種情況:
元素 1 的屬性:
android:id="@ id/view1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
元素 2 的屬性:
android:id="@ id/view2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
第二種情況:
元素 1 的屬性:
android:id="@ id/view1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
元素 2 的屬性:
android:id="@ id/view2"
app:layout_constraintEnd_toEndOf="@ id/view1"
app:layout_constraintStart_toStartOf="@ id/view1"
所有的xml代碼:
第一種情況:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<View
android:id="@ id/view1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/blue"
app:layout_constraintBottom_toTopOf="@ id/view2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/view2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/black"
app:layout_constraintTop_toBottomOf="@ id/view1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
第二種情況:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@ id/view1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/blue"
app:layout_constraintBottom_toTopOf="@ id/view2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@ id/view2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/black"
app:layout_constraintTop_toBottomOf="@ id/view1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/view1"
app:layout_constraintStart_toStartOf="@id/view1"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
總之,將兩個視圖約束到同一個“事物”并將一個視圖約束到“某物”然后將第二個視圖約束到第一個視圖是否相同?哪種情況更好實施,第一種情況還是第二種情況?
uj5u.com熱心網友回復:
目前這兩種情況可能會給你相同的結果。
但是,如果將來您需要在 view1 之外添加另一個視圖(比如 view3)
然后在第一種情況下,它不會對 view2 產生影響,view2 將占用全部空間,即父級寬度但在第二種情況下,由于 view2 依賴于 view1,因此 view2 將僅采用與 view1 相同的寬度,而不是整個父視圖。
我希望這有助于消除您的疑問。
uj5u.com熱心網友回復:
在一種情況下,您將它們限制在父級上,并且它們彼此處于相同的位置。在第二種情況下,您使 View2 依賴于 View1
┌─────────────────────┐ ┌─────────────────────┐
│ │ │ │
│ ┌───────┐ │ │ ┌───────┐ │
│══════│ View1 │══════│ │══════│ View1 │══════│
│ └───────┘ │ │ └───────┘ │
│ │ │ ║ ║ │
│ ┌───────┐ │ │ ┌───────┐ │
│══════│ View2 │══════│ │ │ View2 │ │
│ └───────┘ │ │ └───────┘ │
│ │ │ │
└─────────────────────┘ └─────────────────────┘
什么時候會有不同的表現?如果移動 View1,View 2 也會移動。
┌─────────────────────┐ ┌─────────────────────┐
│ │ │ │
│ ┌───────┐ │ │ ┌───────┐ │
│═══│ View1 │═════════│ │════│ View1 │════════│
│ └───────┘ │ │ └───────┘ │
│ │ │ ║ ║ │
│ ┌───────┐ │ │ ┌───────┐ │
│══════│ View2 │══════│ │ │ View2 │ │
│ └───────┘ │ │ └───────┘ │
│ │ │ │
└─────────────────────┘ └─────────────────────┘
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/531595.html
標籤:安卓xml安卓布局布局
上一篇:Sqlite 安裝操作使用
