ConstraintLayout layout_constraintHorizontal_bias seems not work

蹲街弑〆低调 提交于 2019-12-07 03:33:54

问题


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.

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 !


回答1:


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.




回答2:


Try this:

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


    <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" />
</android.support.constraint.ConstraintLayout>



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/47901955/constraintlayout-layout-constrainthorizontal-bias-seems-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!