ConstraintLayout layout_constraintHorizontal_bias 似乎不起作用

ConstraintLayout layout_constraintHorizontal_bias seems not work

这是我的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:hint="please input test content"
        android:inputType="phone"
        android:textColorHint="@android:color/holo_red_light"
        android:textSize="13sp"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="parent" />
    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.5" />
</android.support.constraint.ConstraintLayout>

下图为预览图

我想将 TextView 移动到 center_horizontal 到父视图,但 layout_constraintHorizontal_bias=0.5" 似乎不起作用。

谁对这个问题有想法?先谢谢了!

试试这个

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:ems="10"
        android:hint="please input test content"
        android:inputType="phone"
        android:textColorHint="@android:color/holo_red_light"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

编辑

要使用 bias,您需要将其约束给任何 parent.

试试这个:

app:layout_constraintHorizontal_bias="0.5" is not working because you haven't constrained it to any parent. Read more about bias.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginBottom="16dp"
        android:ems="10"
        android:hint="please input test content"
        android:inputType="phone"
        android:textColorHint="@android:color/holo_red_light"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

首先,如果我是你,我会看一些解释约束布局的指南,因为你似乎缺少一些关键元素。 ConstraintLayout

话虽如此,您的问题是您要求布局将 0.5 偏差限制为零。您的 textView 不受 app:layout_constraintBottom_toTopOf="parent" 以外的任何限制 顺便说一句,将文本视图的底部限制在父视图的顶部有点奇怪。

为了使偏差起作用,它需要知道要在哪些元素之间产生偏差。如果你只是想让它成为父级的中心,那么像这样将 textview 约束到父级:

<TextView
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="@android:color/black"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.5" />

此外,到那时您将不需要偏差,但我将其保留了下来,因为您随后可以使用它以其他百分比移动它。

不需要偏差作为用于元素之间间距的偏差,因为它不需要一个可以从左下右下到上限制到父级

谢谢