简体   繁体   English

LinearLayout在ScrollView Android中被切断了

[英]LinearLayout is getting cut off inside ScrollView Android

I have an activity in my app where I would like the user to be able to vertically scroll the content contained inside a LinearLayout which in turn is inside a ScrollView . 我在我的应用程序中有一个活动,我希望用户能够垂直滚动LinearLayout中包含的内容,而LinearLayout又包含在ScrollView Here is a summary of what the layout XML for this activity looks like: 以下是此活动的布局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>

What I am observing is that content in the inner LinearLayout is being cutoff inside the ScrollView . 我观察到的是内部LinearLayout中的内容在ScrollView被切断。 In the final TextView above, when it contains little text, then the View below it does render in portrait, but does not in landscape. 在上面的最终TextView ,当它包含很少的文本时,它下面的View会以纵向呈现,但不会在横向上呈现。 When this TextView contains a lot of text, then it gets cutoff in portrait mode. 当此TextView包含大量文本时,它将在纵向模式下截止。

I tried the recommendations I found on Stack Overflow. 我尝试了我在Stack Overflow上找到的建议。 Adding bottom padding to the ScrollView did not resolve the problem, nor did swapping the ScrollView for a NestedScrollView . 添加底部填充到ScrollView没有解决的问题,也没有交换的ScrollViewNestedScrollView

Any helpful suggestions would be welcome. 任何有用的建议都会受到欢迎。 This is actually turning out to be a blocker. 这实际上是一个阻挡者。

Change your inner LinearLayout 's margin to padding. 将内部LinearLayout的边距更改为填充。 Or, if you really need it to be margin (maybe you're using a custom background), wrap your LinearLayout in a FrameLayout . 或者,如果您确实需要它作为边距(可能您使用的是自定义背景),请将您的LinearLayout包装在FrameLayout

The ScrollView is taking its height (or, more accurately, it is computing its scrollable range) from the LinearLayout . ScrollView正在从LinearLayout获取其高度(或者更确切地说,它正在计算其可滚动范围)。 This value doesn't include margins, so your ScrollView is going to be "too short" by the sum of the LinearLayout 's top and bottom margins. 此值不包括边距,因此您的ScrollView将被“ LinearLayout ”的顶部和底部边距的总和“太短”。

The margins are ignored while measuring, See this 边距被忽略,而测量,见

So you can provide padding to your ScrollView and remove margins from your LinearLayout 因此,您可以为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>

Also fill_parent is deprecated and renamed match_parent in API Level 8 and higher 此外,不推荐使用fill_parent ,并在API级别8及更高版本中重命名为match_parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM