LinearLayout在ScrollView Android中被切断了

Tim*_*sen 4 android scrollview android-layout

我在我的应用程序的活动,我希望用户能够垂直滚动里面包含一个内容LinearLayout这又是内部ScrollView.以下是此活动的布局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>
Run Code Online (Sandbox Code Playgroud)

我所观察到的是,内心的LinearLayout内容正在被内部切断ScrollView.在TextView上面的最后一篇中,当它包含很少的文本时,View下面它会以纵向渲染,但不会在横向上渲染.当它TextView包含大量文本时,它会以纵向模式截止.

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

任何有用的建议都会受到欢迎.这实际上是一个阻挡者.

Ben*_* P. 16

将内在LinearLayout的边距改为填充.或者,如果你真的需要它是保证金(也许你正在使用自定义背景),请将你包装LinearLayout成一个FrameLayout.

ScrollView正在其高度(或者,更准确地说,它计算其可滚动范围)从LinearLayout.此值不包括边距,因此您的上边距和下边距ScrollView的总和将"太短" LinearLayout.

  • 换句话说,`ScrollView` 被分配了一个太短的高度,然后当它真正查看它的 `LinearLayout` 时,它停止并截断顶部和底部边距的总和,这在我的案例是40dp。 (3认同)