如何将一对视图置于ConstraintLayout中心?(How do I center a pair of Views inside a ConstraintLayout?)

如何在ConstraintLayout中将一对Views居中,如下所示, 没有嵌套 ? 他们需要触摸,并作为一个单元集中。

我尝试将它们固定在彼此和他们的父母之间但是他们没有相互接触,所以我为两者添加了一个横向偏见,但它没有帮助。

提前致谢...

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>

How do I center a pair of Views within a ConstraintLayout as below, without nesting? They need to be touching, and centered as a single unit.

I tried anchoring them to each other and their parent but then they're not touching each other so I added a horizontal bias for both but it didn't help.

Thanks in advance...

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>

最满意答案

您需要为链的头部设置chainstyle属性,这是水平链中左侧的第一个View 。 将产生预期结果的样式packed 。 在这种情况下不需要偏见。

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>

You need to set a chainstyle attribute for the head of the chain which is the first View on the left in a horizontal chain. The style that will give the expected result is packed. A bias will not be needed in this case.

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>如何将一对视图置于ConstraintLayout中心?(How do I center a pair of Views inside a ConstraintLayout?)

如何在ConstraintLayout中将一对Views居中,如下所示, 没有嵌套 ? 他们需要触摸,并作为一个单元集中。

我尝试将它们固定在彼此和他们的父母之间但是他们没有相互接触,所以我为两者添加了一个横向偏见,但它没有帮助。

提前致谢...

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>

How do I center a pair of Views within a ConstraintLayout as below, without nesting? They need to be touching, and centered as a single unit.

I tried anchoring them to each other and their parent but then they're not touching each other so I added a horizontal bias for both but it didn't help.

Thanks in advance...

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>

最满意答案

您需要为链的头部设置chainstyle属性,这是水平链中左侧的第一个View 。 将产生预期结果的样式packed 。 在这种情况下不需要偏见。

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>

You need to set a chainstyle attribute for the head of the chain which is the first View on the left in a horizontal chain. The style that will give the expected result is packed. A bias will not be needed in this case.

<ImageView android:id="@+id/imageView" android:layout_width="14dp" android:layout_height="14dp" android:scaleType="fitXY" android:src="@drawable/checkmark" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/textView"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/some_text" app:layout_constraintLeft_toRightOf="@id/imageView" app:layout_constraintRight_toRightOf="parent"/>