ConstraintLayout 中 layout_above 的替代方案是什么?

What's the alternative of layout_above in ConstraintLayout?

我试过 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"

您可以使用 高度 0dp垂直组织 视图。

in relative layout 我们可以像

一样使用下面和上面的布局
<FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_nav"
        android:layout_below="@+id/toolbar"
        />

constraint layout 中必须提供高度,因此您可以提供 0 高度。然后你可以像这样将你的视图设置在其他视图下面

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tablayout" />

您可以使用 constraintTop_toBottomOfconstraintBottom_toTopOf 属性将您的视图调整到其他视图的上方或下方. 谢谢