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

编程入门 行业动态 更新时间:2024-10-22 23:39:11
如何将一对视图置于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"/>

更多推荐

本文发布于:2023-08-07 21:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   如何将   中心   pair   Views

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!