Home > Net >  Why does this view ignore its constraints?
Why does this view ignore its constraints?

Time:10-20

The scroll view below ignores the app:layout_constraintTop_toBottomOf="@id/topView" constraint. I want the scroll view to be underneath topView, how can I do that?

<?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"
    tools:context=".MainActivity"">

    <TextView
        android:id="@ id/topView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello!" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toBottomOf="@id/topView"> <!-- has no effect! -->

<LinearLayout
    android:id="@ id/vertical_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintTop_toBottomOf="@id/topView">  <!-- has no effect! -->

    <!-- TextViews in LinearLayout here -->

</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

This is a simple scrollable linear layout.

CodePudding user response:

As said in the comment, you cannot apply a constraint to a view that is not a direct child of the ConstraintLayout.

The second part of why it wasn't doing anything is because you set layout_height of the ScrollView to match_parent so it just forced itself over the TextView.

<?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"
    tools:context=".MainActivity"">

    <TextView
        android:id="@ id/topView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello!"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/topView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"> 

        <LinearLayout
            android:id="@ id/vertical_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            <!-- TextViews in LinearLayout here -->

        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
  • Related