Android 如何为约束布局组指定垂直边距?
所以我有一些观点,我使用constraint layout group将其分组。我想给组提供垂直边距,就像我使用线性/相对布局分组一样。添加android:layout\u margintical=“100dp”似乎不起作用。您试图执行的操作不正确 ConstraintLayout组可用于对不同视图或小部件的某些引用进行“分组”,并设置它们的可见性(您可以设置组的可见性,所有视图都将获得该可见性)。这不是一个像线性布局那样的财务团队 请看一下文档 此类控制一组引用小部件的可见性。 组的可见性将应用于引用的小部件。这是一种方便的方法,可以轻松隐藏/显示一组小部件,而无需以编程方式维护该组小部件Android 如何为约束布局组指定垂直边距?,android,xml,android-layout,view,android-constraintlayout,Android,Xml,Android Layout,View,Android Constraintlayout,所以我有一些观点,我使用constraint layout group将其分组。我想给组提供垂直边距,就像我使用线性/相对布局分组一样。添加android:layout\u margintical=“100dp”似乎不起作用。您试图执行的操作不正确 ConstraintLayout组可用于对不同视图或小部件的某些引用进行“分组”,并设置它们的可见性(您可以设置组的可见性,所有视图都将获得该可见性)。这不是一个像线性布局那样的财务团队 请看一下文档 此类控制一组引用小部件的可见性。 组的可见性将
对于要执行的操作,您应该使用布局(LinearLayout、ConstraintLayout…)并对布局中的视图进行分组,最后为该布局提供所需的边距。组的工作方式与此不同。看看ConstraintLayout自2.0版以来的可用版本。可以搜索有关图层使用的教程。简而言之,图层将允许您对类似于ViewGroup的小部件进行分组,但将保持平面布局结构。如果图层包含的视图可以约束到图层本身,则整个图层将接受上边距 例如,采用以下布局: 蓝色是图层的背景。上/下、右/左视图包含在图层中,并约束在图层上。层上设置了
100dp
的上边距。“外部视图”是一个文本视图,它被约束为“右下角”文本视图,该文本视图包含在图层中,如果图层被视图组替换,则无法执行此操作
视图仍可能包含在组中,以便进行组操作
以下是此布局的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>
嗯,好的,我可以用这个。我觉得不错。非常感谢。