【问题标题】:Nested LinearLayout does not align to parent ScrollView?嵌套的 LinearLayout 不与父 ScrollView 对齐?
【发布时间】:2018-12-11 13:31:53
【问题描述】:

我的片段底部有一个带有两个水平按钮(黄色 = fragment_test_button_container)的片段。我想将剩余区域(red = fragment_test_scrollview)用于 ScrollView 我的 ScrollView 需要由一个布局组成(turquoise strong> = 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>

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

为什么我的绿松石区域悬在蓝色区域的一半?

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 请解释为什么此代码可能有效。
    • 如果能解决您的问题,请接受并支持我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多