【问题标题】:Scrollview not showing last item in layout滚动视图未显示布局中的最后一项
【发布时间】:2019-01-05 13:41:24
【问题描述】:

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

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

<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 时,我的布局变得越来越重。

<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>

【问题讨论】:

  • 什么是android:minHeight=""尺寸值?
  • 您将 LinearLayout 高度设置为 match_parent,因此永远不会滚动。
  • @SushilKumar 它的 ' android:minHeight="40dp" '
  • @dharms 滚动工作,上面有很多项目,但最后一个项目有问题。我对其他布局也有同样的问题。
  • 你不应该在scrollview的子节点上使用匹配父节点并移除权重,这些是主要问题。

标签: android scrollview android-linearlayout android-scrollview


【解决方案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>

【讨论】:

    【解决方案2】:

    这是因为 layout_height="match_parent" 用于 ScrollView 子 LinearLayout 将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

    将 View 的大小设置为 wrap_content 会强制它展开到足以包含它所包含的值(或子控件)的程度。对于控件——如文本框 (TextView) 或图像 (ImageView)——这将包装正在显示的文本或图像。对于布局元素,它将调整布局大小以适应作为其子元素添加的控件/布局。

    Official Docs Says

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

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

    【讨论】:

    • 你删除了android:weightSum="1"
    • @Kishan 并将 ScrollView 的所有子项替换为 attr android:layout_height="0dp" wrap_contentmatch_parent
    • 我必须使用 layout_height 因为没有它我无法进行布局,而且我还使用了框架布局。
    • @Kishan 你删除了 android:weightSum="1"
    • 我从 ScrollView => LinearLayout => LinearLayout [from here i remove] 中删除“weightSum=1”是的,但问题是我的布局变得一团糟......
    【解决方案3】:

    最简单的解决方案就是像这样将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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多