簡體   English   中英

如何為約束布局組提供垂直邊距?

[英]How to give vertical margin to constraint layout group?

所以我有一些視圖,我使用約束布局組對其進行分組。 如果我使用線性/相對布局對其進行分組,我想為該組提供垂直邊距。 添加android:layout_marginVertical="100dp"似乎不起作用。

團體不是那樣工作的。 看看自 2.0 版以來可用的 ConstraintLayout 的圖層小部件 您可以搜索有關使用Layer 的教程。 簡而言之, Layer將允許您對類似於ViewGroup 的小部件進行分組,但將保持平面布局結構。 如果圖層中包含的視圖可以被約束到圖層本身,則整個圖層將接受一個上邊距。

例如,采用以下布局:

在此處輸入圖片說明

藍色是圖層的背景。 頂/底、右/左視圖包含在圖層內並受其約束。 Layer上設置了100dp的上邊距。 “外部視圖”是一個TextView ,它被限制在“右下角” TextVIew 中,它包含在Layer 中,如果LayerViewGroup替換,則無法完成。

視圖可能仍包含在組中以進行組操作。

這是此布局的 XML:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.helper.widget.Layer
        android:id="@+id/layer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="100dp"
        android:background="@android:color/holo_blue_light"
        app:constraint_referenced_ids="topLeft,topRight,bottomLeft,bottomRight"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/topLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Top left"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toStartOf="@id/layer"
        app:layout_constraintTop_toTopOf="@id/layer" />

    <TextView
        android:id="@+id/topRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Top right"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toStartOf="@+id/bottomRight"
        app:layout_constraintTop_toTopOf="@id/layer" />

    <TextView
        android:id="@+id/bottomLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Bottom left"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toStartOf="@id/layer"
        app:layout_constraintTop_toBottomOf="@+id/topLeft" />

    <TextView
        android:id="@+id/bottomRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Bottom right"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintStart_toEndOf="@+id/bottomLeft"
        app:layout_constraintTop_toBottomOf="@+id/topRight" />

    <TextView
        android:id="@+id/outsideView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Outside view"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bottomRight" />

</androidx.constraintlayout.widget.ConstraintLayout>

你試圖做的是不正確的。

ConstraintLayout Group 可用於“分組”不同視圖或小部件的一些引用並設置它們的可見性(您可以設置組的可見性,所有視圖都會得到它)。 它不是像 LinearLayout 那樣的財政組。

請看一下文檔

https://developer.android.com/reference/androidx/constraintlayout/widget/Group

此類控制一組引用小部件的可見性。 該組的可見性將應用於引用的小部件。 這是一種輕松隱藏/顯示一組小部件而無需以編程方式維護該組的便捷方式。

對於您想要做的事情,您應該使用布局(LinearLayout、ConstraintLayout...)並將該視圖分組在布局內,最后為該布局提供所需的邊距。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM