android - 如何在 ConstraintLayout 中将一对 View 居中?

标签 android android-constraintlayout

如何在 ConstraintLayout 中将一对 View 居中,如下所示,没有嵌套?它们需要接触并作为一个整体居中。

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"/>

最佳答案

您需要为链头设置 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"/>

关于android - 如何在 ConstraintLayout 中将一对 View 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51200222/

相关文章:

android - ConstraintLayout 中出现额外的边距

android - ConstraintLayout 与 RelativeLayout 和 CardView - 缓存和内存空间

java - 媒体编解码器工作不顺利

java - 使用 HttpClient 发布 JSONObject 时出错

android - 如何让两个TextView彼此对齐为 "wrap_content"但又在ConstraintLayout的约束范围内而不互相溢出?

java - 如何在约束布局中创建容器以便设置背景?

android - 约束布局包含问题

java - 在 JDK1.8 上解析 android-21 的数据失败

android - PhoneGap Android 获取应用版本代码

android - 如何在 Android 中更改 SettingsActivity 的主题?