(安卓)ScrollView无法滚动到我的LinearLayout的底部

9

我有一个内部包含LinearLayout的ScrollView。当我尝试滚动到我的linearlayout底部时,似乎会裁剪掉底部的5dip(即底部边距)。我认为这可能与我的LinearLayout的5dip边距有关?

这是activity_create_account.xml

:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@drawable/grey"
android:orientation="vertical"
android:padding="0dp"
tools:context=".Login" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100"
android:orientation="vertical">

<!-- BEGIN HEADER -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/titlebar"
    android:orientation="horizontal"
    android:padding="8dip" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="0dp"
        android:text="create account"
        android:textColor="#FFFFFF"
        android:textSize="32sp"
        android:textStyle="bold"
        android:typeface="sans" />
</LinearLayout>
<!-- END HEADER -->

<!-- BEGIN BODY -->

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:orientation="vertical">

<LinearLayout
    android:id="@+id/innerLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:background="@drawable/rounded_white"
    android:orientation="vertical"
    android:padding="5dip" >

    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
        android:id="@+id/etChooseUsername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Choose a username"
        android:inputType="text"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />
    <EditText
        android:id="@+id/etChoosePassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Choose a password"
        android:inputType="textPassword"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />
    <EditText
        android:id="@+id/etRetypePassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Re-type password"
        android:inputType="textPassword"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />

    <Button
        android:id="@+id/bCreateAccountConfirm"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_weight="1"
        android:background="@drawable/button_selector"
        android:padding="0dip"
        android:text="Create Account"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:typeface="sans" />
</LinearLayout>
</ScrollView>
<!-- END BODY -->

这是我试图向下滚动时的效果(按钮下方应该有一个薄白色区域和一个薄灰色边距)。 输入图片说明

1
android:layout_margin="10dip"android:id="@+id/innerLinearLayout"中移除,改为在ScrollView上添加android:padding="10dip" - bogdan
@bogdan 当我在我的ScrollView上放置padding时,它仍然会截断我的内部布局底部(并且无论滚动到哪里都会留下一个难看的10dip宽度的灰色区域)。 - Aaron Mampáro
我可能有点误读问题,忘记了我关于ScrollView的10dp填充的更改,抱歉。您希望“创建帐户”按钮在底部有一个小边距,因此请在android:id="@+id/bCreateAccountConfirm"上放置android:layout_marginBottom="10dp" - bogdan
1
在我的应用程序中,我在LinearLayout中设置了marginTop,出现了相同的问题。然后我设置了paddingTop,它起作用了。我认为这是一个bug,因为marginTop会使布局完全向下移动! - user7301143
5个回答

18

我意识到这个问题有点旧了 - 但是为了那些通过Google找到这个问题的人着想,我会尽力阐述一下。

似乎通用的ScrollView对象在与其他兄弟姐妹控件一起使用时会出现问题。因此,谷歌创建了一个NestedScrollView。从文档中可以看出,

NestedScrollView与ScrollView非常相似,但它支持在新旧版本的Android上充当嵌套滚动的父级和子级。

我遇到过类似于这个问题的情况,使用NestedScrollView消除了原始ScrollView底部的截断问题。


2
谢谢!帮我省了很多时间,而且运行得很好。 - Jakob Abfalter
很遗憾,这导致我的应用程序崩溃了。不确定原因。有什么想法吗? - Jonathan

11

在你的ScrollView中添加padding_bottom值为10dp。它会生效。

否则,位于HorizontalView下面的视图可能会覆盖在这个水平视图上方。在这种情况下,

  1. 将一个id添加到HorizontalView id="@+id/horizontalView"
  2. 在水平视图下面的视图中添加 below="@+id/horizontalView"

非常感谢你,Naresh :) - Talha Q
“HorizontalView”是什么意思? - rmtheis
我猜它可能已经被改成了“HorizontalScrollView”。 - Naresh
为什么添加填充是必要的?我理解它的工作原理,但为什么scrollview一开始就出了问题?编辑:我通过将scrollview底部约束到父级底部来解决此问题。所以我猜测视图比父级更大。 - lucidbrot

4
您可以将 marginBottom 设置为您的 scrollview。
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:layout_marginBottom="10dp"
    android:orientation="vertical">

3
我曾遇到类似的问题,原因是滚动视图内文本视图的边距设置。将文本视图的边距去掉,在滚动视图中添加边距后,所有文本都能正常显示并且滚动也能按预期工作。在我的情况下,我设了20dp的边距,导致整行文本无法显示完全,所以我把底部的边距从文本视图中去掉,并添加到滚动视图中。 - WallyHale
在我的情况下,原因是TableLayout使用高度为2dp的分隔线。我有30行,所以我必须添加60dp的marginBottom,它正好滚动到最后,包括最后一行。 - maximus
这有点像是个技巧,但它能够工作,并且相比于弄清楚 Android 渲染引擎的“巫术行为”,这是最简单的解决方案。 - angryITguy

3
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"

真正为我解决了问题,不需要使用NestedView或padding。但要保持ScrollView的直接子视图的layout_height为“wrapped_content”。


0
一个短暂的延迟解决了我的问题,似乎是因为在添加项后立即告诉视图滚动到底部可能会错过新项并滚动一个短距离。
        mView.postDelayed(new Runnable() {
            @Override
            public void run() {
                sv.fullScroll(View.FOCUS_DOWN);
            }
        },200);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接