片段视图不填充整个片段空间

时间:2018-01-31 19:15:00

标签: android android-layout android-fragments

我有一个由三个子片段组成的片段:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <fragment
        android:id="@+id/header"
        class="xxx.xxx.xxx.HeaderFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <fragment
        android:id="@+id/content"
        class="xxx.xxx.xxx.ContentFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/footer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header" />

    <fragment
        android:id="@+id/footer"
        class="xxx.xxx.xxx.FooterFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxHeight="64dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>

这样,ContentFragment填充了HeaderFragment和FooterFragment之间的空间,如下图所示:

green = header

蓝色=内容

red = footer

enter image description here

现在我想为ContentFragment布局添加两个视图(一个TextView和一个ScrollView),这样这些视图就可以获得整个蓝色空间。

以下是ContentFragment的布局:

<?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:background="@color/lightBlue">

    <TextView
        android:background="@color/white"
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="@id/right"
        app:layout_constraintStart_toStartOf="@id/left"
        app:layout_constraintTop_toTopOf="parent"/>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@id/right"
        app:layout_constraintStart_toStartOf="@id/left"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <TextView
            android:id="@+id/veryLongTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </ScrollView>

    <android.support.constraint.Guideline
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.05" />

    <android.support.constraint.Guideline
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.95" />

</android.support.constraint.ConstraintLayout>

这就是我得到的:

enter image description here

有人可以解释为什么在滚动视图的底部和蓝色部分的底部之间有一些空间?它不是我在约束中定义的,它在Android Studio中呈现如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

我仍然不确定为什么会这样。我的猜测是Android无法在运行时为ContentFragment计算正确的高度,因为如果我使用Guidelines定义片段高度问题就会消失,如下所示:

    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <fragment
            android:id="@+id/header"
            class="xxx.xxx.xxx.HeaderFragment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@+id/topLimit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <fragment
            android:id="@+id/content"
            class="xxx.xxx.xxx.ContentFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottomLimit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/topLimit" />

        <fragment
            android:id="@+id/footer"
            class="xxx.xxx.xxx.FooterFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/bottomLimit"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

        <android.support.constraint.Guideline
            android:id="@+id/topLimit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.15" />

        <android.support.constraint.Guideline
            android:id="@+id/bottomLimit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.85" />
    </android.support.constraint.ConstraintLayout>

这是这样渲染的:

enter image description here