如何在ConstraintLayout中居中一对View?

5

如何在不嵌套的情况下将以下一对Views居中于ConstraintLayout中?它们需要紧密相连,并作为单个单元居中。

enter image description here

我尝试将它们锚定到彼此和它们的父级,但它们之间没有接触,所以我为两者都添加了水平偏差,但这并没有帮助。
提前感谢您...
<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"/>

3
使用“packed chain”即可。 - CommonsWare
@CommonsWare 是的,它成功了。谢谢。 - Barry Fruitman
1个回答

6

您需要为水平链中最左边的第一个View设置一个chainstyle属性。可以使用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"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接