LinearLayout 在 ScrollView 中被截断 Android

LinearLayout is getting cut off inside ScrollView Android

我的应用程序中有一个 activity,我希望用户能够垂直滚动 LinearLayout 中包含的内容,而 LinearLayout 又位于 ScrollView 中。以下是此 activity 的布局 XML 的摘要:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dip"
            android:orientation="vertical">

            <!-- a bunch of standard widgets, omitted for brevity -->

            <!-- everything renders but starting in this TextView -->
            <!-- content begins to get truncated -->
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:gravity="left"
                android:textSize="20dip"/>

            <!-- this View, really just a trick for a horizontal row, is -->
            <!-- completely cutoff -->
            <View
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:background="@color/green" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

我观察到内部 LinearLayout 中的内容在 ScrollView 中被截断。在上面最后的 TextView 中,当它包含很少的文本时,下面的 View 会以纵向呈现,但不会以横向呈现。当此 TextView 包含大量文本时,它会在纵向模式下被截断。

我尝试了在 Stack Overflow 上找到的建议。将底部填充添加到 ScrollView 并没有解决问题,也没有将 ScrollView 换成 NestedScrollView.

欢迎提出任何有用的建议。这实际上是一个障碍。

将内部 LinearLayout 的边距更改为填充。或者,如果您真的需要它作为边距(也许您正在使用自定义背景),请将您的 LinearLayout 包装在 FrameLayout.

ScrollView 正在从 LinearLayout 获取其高度(或者更准确地说,它正在计算其可滚动范围)。此值不包括边距,因此您的 ScrollView 将是 "too short" 乘以 LinearLayout 的顶部和底部边距之和。

测量时忽略边距,参见this

因此您可以为 ScrollView 提供填充并从 LinearLayout

中删除边距
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- a bunch of standard widgets, omitted for brevity -->

        <!-- everything renders but starting in this TextView -->
        <!-- content begins to get truncated -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:gravity="left"
            android:textSize="20dip"/>

        <!-- this View, really just a trick for a horizontal row, is -->
        <!-- completely cutoff -->
        <View
            android:layout_width="match_parent"
            android:layout_height="2dip"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@color/green" />
    </LinearLayout>
</ScrollView>

另外 fill_parent 在 API 8 级及更高级别

中被弃用并重命名为 match_parent