android – ConstraintLayout中layout_above的替代方法是什么?

我试过app:layout_constraintBottom_toTopOf =“@ id / the_view_which_will_remain_below”,但它不是所需的输出.

我想从RelativeLayout的layout_above中获得相同的行为.

使用此代码,我的Textview开始从底部出现,随着字符增加文本进入上面.但我希望文本从父母的开始开始.

<?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"
    android:padding="16dp"
    tools:context="com.fatimamostafa.restfulwebservices.asynctask.AsyncTaskRequest">
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Run" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Clear" />

    </LinearLayout>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/ll"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textSize="160sp"
        android:text="Text"
      />


</android.support.constraint.ConstraintLayout>

最佳答案 要将文本放在父级的顶部,将LinearLayout放在底部,将文本视图的顶部绑定到父级,并从LinearLayout取消约束它:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text"
    android:textSize="160sp"

    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"

    app:layout_constraintTop_toTopOf="parent"

     />

<LinearLayout
    android:id="@+id/ll"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
   >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Run" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Clear" />

</LinearLayout>

我删除了一些我认为多余的约束.

另请注意,您可以使用“@ id”代替@id将TextView放在LinearLayout上.

如果要约束在父顶部和线性布局之间浮动的文本,请将这些约束添加到TextView,并调整0到1之间的垂直偏差.

app:layout_constraintBottom_toTopOf="@+id/ll"
app:layout_constraintVertical_bias="0.2"
点赞