Cyrus
Cyrus

Reputation: 9425

ConstraintLayout layout_constraintHorizontal_bias seems not work

This is my layout file.

<?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>

Follow pic is preview view.

enter image description here

I want to move TextView to center_horizontal to parent view , but layout_constraintHorizontal_bias=0.5" seems not work.

Who has ideas for this problem ? Thanks for first !

Upvotes: 10

Views: 29581

Answers (4)

Vidhi Dave
Vidhi Dave

Reputation: 5684

Try this:

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

enter image description here

<?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>

Upvotes: 5

blackHawk
blackHawk

Reputation: 6307

There is no need of bias as bias used for spacing between elements since its not require one can constaint to parent from left right bottom and top

Thanks

Upvotes: -2

AskNilesh
AskNilesh

Reputation: 69709

Try this

<?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>

EDIT

to use bias you need to give constrained it to any parent.

enter image description here

Upvotes: 6

Jesper Purup
Jesper Purup

Reputation: 394

First of if i were you i would look at some guides explaining constraint layouts, since you seem to be missing some key elements. ConstraintLayout

When that is said, your problem is that you are asking your layout to constrain at a 0.5 bias to nothing. Your textView is not constrained to anything other than app:layout_constraintBottom_toTopOf="parent" Which btw is a little wierd constraining the bottom of your textview to the top of your parent.

In order for bias to work, it needs to know which elements to be biased between. If you simple want it to be center of the parent then constrain the textview to the parent like so:

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

Also at that point you wont need the bias, i left it in though since you could then use it to move it at other percentages.

Upvotes: 3

Related Questions