【问题标题】:How to give vertical margin to constraint layout group?如何为约束布局组提供垂直边距?
【发布时间】:2021-01-10 09:26:40
【问题描述】:

所以我有几个视图,我使用约束布局组对其进行分组。如果我使用线性/相对布局对其进行分组,我想为该组提供垂直边距。添加android:layout_marginVertical="100dp" 似乎不起作用。

【问题讨论】:

    标签: android xml android-layout view android-constraintlayout


    【解决方案1】:

    群组不是这样工作的。看看 ConstraintLayout 的 Layer widget 从 2.0 版开始可用。你可以搜索Layer的使用教程。简而言之,Layer 将允许您对类似于 ViewGroup 的小部件进行分组,但会保持平面布局结构。如果图层包含的视图可以被限制到图层本身,则整个图层将接受一个上边距。

    例如,采取如下布局:

    蓝色是图层的背景。顶/底、右/左视图包含在图层内并被限制在图层内。 100dp 的上边距设置在 Layer 上。 “外部视图”是一个 TextView,它被限制在“右下”TextVIew,它包含在 Layer 中,这是不可能的如果 LayerViewGroup 替换,则完成。

    视图可能仍包含在 Group 中以进行组操作。

    这是此布局的 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>
    

    【讨论】:

    • 嗯好吧,我可以使用它。这对我来说似乎很好。谢谢你..
    【解决方案2】:

    您尝试做的事情不正确。

    ConstraintLayout Group 可用于“分组”不同视图或小部件的一些引用并设置它们的可见性(您可以设置组的可见性,所有视图都会得到它)。它不是一个像 LinearLayout 那样的财政组。

    请看文档

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

    该类控制一组引用小部件的可见性。 该组的可见性将应用于引用的小部件。这是一种轻松隐藏/显示一组小部件的便捷方式,无需以编程方式维护该组。

    对于您想要做的事情,您应该使用布局(LinearLayout、ConstraintLayout...)并将该视图分组到布局内,最后为该布局提供所需的边距。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多