滚动视图不显示布局中的最后一项

时间:2019-01-05 13:41:25

标签: android scrollview android-linearlayout android-scrollview

这是我正面临的问题的代码,我在滚动视图中采用了2线性布局,现在第二线性布局中的最后一个按钮被隐藏了,或者我可以说显示不正确。

这是屏幕截图。 您可以检查我选择的区域是否为按钮,但不能正常工作。

enter image description here

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <LinearLayout
            android:id="@+id/ConcernedPortionofScreen"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25"
            android:orientation="vertical">
        </LinearLayout>

        <LinearLayout
            android:id="@+id/RestofScreen"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.75"
            android:orientation="vertical"
            android:padding="20dp">               

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/custom_button_shape_with_bg_primary"
                android:minHeight="@dimen/minimum_height_all_object"
                android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                android:text="SELECT"
                android:textColor="@color/white"
                />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

当我尝试删除layout_weight时,我的布局越来越笨重。

enter image description here

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <LinearLayout
                android:id="@+id/ConcernedPortionofScreen"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.25"
                android:orientation="vertical">

                <!-- Parent FrameLayout 'FL' -->
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <!--Conitans Tree Childs -->

                </FrameLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/RestofScreen"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.75"
                android:orientation="vertical"
                android:padding="20dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:weightSum="1">

                    <!--Conitans Two Childs -->

                </LinearLayout>


                <EditText
                    style="@style/CustomEdittextTheme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                    android:hint="Enter Child Name"
                    android:minHeight="@dimen/add_child_rquest_minimum_height_all_object" />

                <EditText
                    style="@style/CustomEdittextTheme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                    android:hint="Select City"
                    android:minHeight="@dimen/add_child_rquest_minimum_height_all_object" />

                <TextView
                    style="@style/CustomEdittextTheme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                    android:hint="Select Birthdate"
                    android:minHeight="@dimen/add_child_rquest_minimum_height_all_object" />

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

                    <!--Conitans Two Childs -->

                </LinearLayout>

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

                    <!--Conitans Two Childs -->

                </LinearLayout>


                <EditText
                    style="@style/CustomEdittextTheme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                    android:hint="Enter Mobile Number"
                    android:minHeight="@dimen/add_child_rquest_minimum_height_all_object" />

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

                    <!--Conitans Two Childs -->

                </LinearLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                    android:fontFamily="@font/segoe_ui_bold"
                    android:paddingLeft="10dp"
                    android:text="Note / Message :"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="18dp" />

                <EditText
                    style="@style/CustomEdittextTheme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                    android:inputType="textMultiLine"
                    android:lines="8"
                    android:maxLines="8"
                    android:minHeight="@dimen/add_child_rquest_minimum_height_all_object"
                    android:minLines="8"
                    android:scrollbars="vertical" />


                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="@dimen/add_child_rquest_margin_top"
                    android:background="@drawable/custom_button_shape_with_bg_primary"
                    android:fontFamily="@font/segoe_ui_bold"
                    android:minHeight="@dimen/add_child_rquest_minimum_height_all_object"
                    android:text="SUBMIT"
                    android:textColor="@color/white" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:1)

伙计们,我刚刚删除了Main Linear_layout,它现在运行良好。

谢谢你们的回答。

这是最适合我的代码。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <!-- Part 1 [layout_weight='0.3' ] -->
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

                <!-- Contains Childs -->

            </FrameLayout>

            <!-- Part 2 [layout_weight='0.7' ] -->
            <LinearLayout
                android:id="@+id/RestofScreen"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.75"
                android:orientation="vertical"
                android:padding="20dp">

                <!-- Contains All Childs -->

            </LinearLayout>

        </LinearLayout>

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

答案 1 :(得分:0)

发生这种情况是因为ScrollView子级LinearLayout的layout_height="match_parent"layout_height更改为wrap_content

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    >

    <LinearLayout
        android:layout_width="match_parent"
        //android:layout_height="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

    //childs ..........
    </LinearLayout>
<ScrollView>

wrap_content

将视图的大小设置为wrap_content会强制其仅扩展至足以容纳其包含的值(或子控件)的程度。对于控件-例如文本框(TextView)或图像(ImageView)-这将包裹所显示的文本或图像。对于布局元素,它将调整布局的大小以适合作为其子级添加的控件/布局。

Official Docs Says

  • FILL_PARENT(在API级别8和更高版本中重命名为MATCH_PARENT),这意味着该视图希望为big as its parent(减去填充)

  • WRAP_CONTENT,这意味着视图要大一些enough to enclose its content(加上填充)

答案 2 :(得分:0)

最简单的解决方案就是这样将ConstraintLayout放在ScrollView

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:overScrollMode="never">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/constraintLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:baselineAligned="false"
                    android:orientation="vertical">

<!--            your other views could be here-->
<!--            your other views could be here-->
                </LinearLayout>
            </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>