嵌套的LinearLayout不能与父级ScrollView对齐?

时间:2018-12-11 13:31:53

标签: android-layout android-fragments android-linearlayout android-constraintlayout

我的片段底部有两个水平按钮(黄色 = fragment_test_button_container )。我想将剩余区域(红色 = fragment_test_scrollview )用于ScrollView fragment_test。我的ScrollView必须包含一种布局(绿松石 = fragment_test_check )。然后,这又可以具有进一步的布局,如您在所附的fragment_test.xml

中所见
<?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"
    tools:context=".ui.TestFragment">

    <ScrollView
        android:id="@+id/fragment_test_scrollview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/fragment_test_button_container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent=".84"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/fragment_test_check"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/fragment_test_front_photo_container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".28"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/fragment_test_front_photo_title"
                    style="@style/myapp_MediumTextStyle"
                    android:layout_width="@dimen/myapp_test_fragment_textview_width"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="24dp"
                    android:text="@string/fragment_test_front_photo_title_text" />

                <ImageView
                    android:id="@+id/fragment_test_front_photo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/fragment_test_back_photo_container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".28"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/fragment_test_back_photo_title"
                    style="@style/myapp_MediumTextStyle"
                    android:layout_width="@dimen/myapp_test_fragment_textview_width"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="24dp"
                    android:text="@string/fragment_test_back_photo_title_text" />

                <ImageView
                    android:id="@+id/fragment_test_back_photo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical" />

            </LinearLayout>

            <!-- ADDITIONAL DATA -->
            <LinearLayout
                android:id="@+id/fragment_test_data_container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".34"
                android:orientation="vertical"
                android:paddingStart="24dp"
                android:paddingEnd="24dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/fragment_test_value1_label"
                        style="@style/myapp_MediumTextStyle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text=„Value1^“ />

                    <TextView
                        android:id="@+id/fragment_test_value1_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="24dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/fragment_test_value2_label"
                        style="@style/myapp_MediumTextStyle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text=„Value2“ />

                    <TextView
                        android:id="@+id/fragment_test_value2_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="24dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/fragment_test_value3_label"
                        style="@style/myapp_MediumTextStyle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text="Ausstellungsdatum" />

                    <TextView
                        android:id="@+id/fragment_test_value3_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="24dp" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/fragment_test_button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingStart="24dp"
        android:paddingEnd="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fragment_test_scrollview">

        <Button
            android:id="@+id/fragment_test_back_button"
            style="@style/myapp_ContinueBackButtonStyle"
            android:layout_width="0dp"
            android:layout_height="@dimen/myapp_continueBackButton_height"
            android:layout_weight="1"
            android:background="@color/myapp_colorPrimary"
            android:text=„back“
            android:textColor="#FFFFFF" />

        <Button
            android:id="@+id/fragment_test_next_button"
            style="@style/myapp_ContinueBackButtonStyle"
            android:layout_width="0dp"
            android:layout_height="@dimen/myapp_continueBackButton_height"
            android:layout_marginStart="24dpVerySmall"
            android:layout_weight="1"
            android:background="@color/myapp_colorPrimary"
            android:text="continue"
            android:textColor="#FFFFFF" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>

但是我的turqoise(fragment_test_check)区域与蓝色区域(fragment_test_scrollview)没有完全对齐,尽管我写了match_parent。我想把turqoise地区的高处作为蓝色地区。

为什么我的turqoise区域悬在蓝色区域的一半?

1 个答案:

答案 0 :(得分:0)

在您的android:fillViewport="true"中设置ScrollView

设置fillViewport时,

true会将内容的高度拉伸到视口的边界。简单来说,fillViewport决定布局的嵌套内容是否应与父级的边界匹配。因此,要使嵌套布局与父级的边界匹配,请将fillViewport设置为true

此外,嵌套组件的layout_gravity应该为1或您在父布局的weight_sum中定义的任何值,而在您的情况下为0.90。