Android ScrollView拒绝滚动到底部

时间:2022-08-24 23:44:22

I have a root scrollview element with a relativelayout in it, and a bunch of form elements inside the relative layout.

我有一个root scrollview元素,其中包含一个relativelayout,以及相对布局中的一堆表单元素。

For some reason, when the soft keyboard is up it seems unable to scroll all the way to the bottom, which cuts one of my buttons in half.

出于某种原因,当软键盘启动时,它似乎无法一直滚动到底部,这将我的一个按钮切成两半。

Here is a screenshot of the hierarchy viewer to demonstrate what I mean.

以下是层次结构查看器的屏幕截图,用于演示我的意思。

Android ScrollView拒绝滚动到底部

As you can see, the system knows that the view continues past the keyboard, yet the scrollview (which fills the visible part of the screen correctly) won't continue to scroll down as it should.

如您所见,系统知道视图继续通过键盘,但滚动视图(正确填充屏幕的可见部分)将不会继续向下滚动。

I have android:windowSoftInputMode="adjustResize" in the manifest for the activity, and I can/will not switch it to pan.

我在活动清单中有android:windowSoftInputMode =“adjustResize”,我可以/不会将其切换为pan。

Any help is appreciated.

任何帮助表示赞赏。

edit: I am seeing this in more than 1 view. Here is the xml of another view with the same problem:

编辑:我在多个视图中看到这个。这是另一个具有相同问题的视图的xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp" >
        <EditText
            android:id="@+id/reset_oldpass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:ems="10"
            android:singleLine="true"
            android:hint="@string/current_password"
            android:layout_marginTop="16dp" />
        <EditText
            android:id="@+id/reset_pass1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/reset_oldpass"
            android:ems="10"
            android:hint="@string/reset_new_pass"
            android:inputType="textPassword"
            android:layout_marginTop="16dp" />
        <EditText
            android:id="@+id/reset_pass2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/reset_pass1"
            android:ems="10"
            android:hint="@string/reset_confirm_pass"
            android:inputType="textPassword"
            android:layout_marginTop="16dp" />
        <TextView
            android:id="@+id/reset_forgot_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/reset_pass2"
            android:layout_marginTop="16dp"
            android:textColor="@color/Link"
            android:textStyle="bold"
            android:text="@string/Login_forgot_password" />
        <Button
            android:id="@+id/reset_reset_password_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/reset_forgot_password"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="32dp"
            android:text="@string/reset_change_pass" />
    </RelativeLayout>
</ScrollView>

5 个解决方案

#1


40  

This is truly strange, but it seems to be caused by android:layout_margin="32dp" within the RelativeLayout. Once I took it out the scroll worked properly.

这真的很奇怪,但它似乎是由RelativeLayout中的android:layout_margin =“32dp”引起的。一旦我拿出它,滚动工作正常。

Of course, because of this I had to add a bunch more margins to my form elements, but at least this is now fixed.

当然,因为这个我不得不为我的表单元素添加更多的边距,但至少现在已经修复了。

#2


13  

I had the same issue and I fixed it by using padding in the ScrollView instead of margin in RelativeLayout.

我有同样的问题,我通过使用ScrollView中的填充而不是RelativeLayout中的边距来修复它。

So, remove android:layout_margin="32dp" from RelativeLayout(the child of ScrollView) and add android:padding="32dp" to the ScrollView.

因此,从RelativeLayout(ScrollView的子节点)中删除android:layout_margin =“32dp”并将android:padding =“32dp”添加到ScrollView。

#3


0  

You can also insert a false view to imitate the bottom margin like this:

您还可以插入一个错误视图来模仿底部边距,如下所示:

<View
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_below="@id/recompensaLabel"
            android:layout_marginTop="0dp"
            android:id="@+id/simulate_bottom_margin_view"

            />

The effect is the same, you only have to insert it below the last element in your Relative Layout.

效果是一样的,您只需将它插入相对布局中的最后一个元素下面。

#4


0  

I just had this problem with an app and found out that it was due to the ScrollView layout_height being set to wrap content. The issue is that if you have enough content to need a ScrollView, you don't want to wrap the content because the bottom of the ScrollView is off the screen by some arbitrary amount. I didn't have enough data to be affected by this as I didn't need a ScrollView until the keyboard popped up. By the looks of it, the OP was in the same situation.

我刚刚在应用程序中遇到此问题,并发现它是由于ScrollView layout_height被设置为包装内容。问题是如果你有足够的内容需要ScrollView,你不想包装内容,因为ScrollView的底部离开了屏幕任意数量。我没有足够的数据受此影响因为我不需要ScrollView直到键盘弹出。从它的外观来看,OP处于相同的情况。

Now, once the keyboard pops up, the bottom of the ScrollView is raised the same amount as the height of the keyboard and ends up below the top of the keyboard by the same arbitrary amount that it was below the bottom of the visible screen.

现在,一旦弹出键盘,ScrollView的底部就会升高与键盘高度相同的数量,并且最终会低于键盘顶部,与可见屏幕底部下方的任意数量相同。

Ultimately, my fix was to bind the top of the ScrollView to my toolbar and then right-click the ScrollVIew in the tree and select 'center vertically' (not in parent option). Now works like a charm. The layout_height should automatically go to 0dp.

最后,我的修复是将ScrollView的顶部绑定到我的工具栏,然后右键单击树中的ScrollVIew并选择“垂直居中”(不在父选项中)。现在就像一个魅力。 layout_height应自动转到0dp。

#5


0  

Making status bar transparent in lollipop and above was creating the problem for me. Setting

在棒棒糖及以上制作状态栏是透明的,这对我来说是个问题。设置

android:fitsSystemWindows="true"
to the parent view of layout containing scrollView solved the issue.

#1


40  

This is truly strange, but it seems to be caused by android:layout_margin="32dp" within the RelativeLayout. Once I took it out the scroll worked properly.

这真的很奇怪,但它似乎是由RelativeLayout中的android:layout_margin =“32dp”引起的。一旦我拿出它,滚动工作正常。

Of course, because of this I had to add a bunch more margins to my form elements, but at least this is now fixed.

当然,因为这个我不得不为我的表单元素添加更多的边距,但至少现在已经修复了。

#2


13  

I had the same issue and I fixed it by using padding in the ScrollView instead of margin in RelativeLayout.

我有同样的问题,我通过使用ScrollView中的填充而不是RelativeLayout中的边距来修复它。

So, remove android:layout_margin="32dp" from RelativeLayout(the child of ScrollView) and add android:padding="32dp" to the ScrollView.

因此,从RelativeLayout(ScrollView的子节点)中删除android:layout_margin =“32dp”并将android:padding =“32dp”添加到ScrollView。

#3


0  

You can also insert a false view to imitate the bottom margin like this:

您还可以插入一个错误视图来模仿底部边距,如下所示:

<View
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_below="@id/recompensaLabel"
            android:layout_marginTop="0dp"
            android:id="@+id/simulate_bottom_margin_view"

            />

The effect is the same, you only have to insert it below the last element in your Relative Layout.

效果是一样的,您只需将它插入相对布局中的最后一个元素下面。

#4


0  

I just had this problem with an app and found out that it was due to the ScrollView layout_height being set to wrap content. The issue is that if you have enough content to need a ScrollView, you don't want to wrap the content because the bottom of the ScrollView is off the screen by some arbitrary amount. I didn't have enough data to be affected by this as I didn't need a ScrollView until the keyboard popped up. By the looks of it, the OP was in the same situation.

我刚刚在应用程序中遇到此问题,并发现它是由于ScrollView layout_height被设置为包装内容。问题是如果你有足够的内容需要ScrollView,你不想包装内容,因为ScrollView的底部离开了屏幕任意数量。我没有足够的数据受此影响因为我不需要ScrollView直到键盘弹出。从它的外观来看,OP处于相同的情况。

Now, once the keyboard pops up, the bottom of the ScrollView is raised the same amount as the height of the keyboard and ends up below the top of the keyboard by the same arbitrary amount that it was below the bottom of the visible screen.

现在,一旦弹出键盘,ScrollView的底部就会升高与键盘高度相同的数量,并且最终会低于键盘顶部,与可见屏幕底部下方的任意数量相同。

Ultimately, my fix was to bind the top of the ScrollView to my toolbar and then right-click the ScrollVIew in the tree and select 'center vertically' (not in parent option). Now works like a charm. The layout_height should automatically go to 0dp.

最后,我的修复是将ScrollView的顶部绑定到我的工具栏,然后右键单击树中的ScrollVIew并选择“垂直居中”(不在父选项中)。现在就像一个魅力。 layout_height应自动转到0dp。

#5


0  

Making status bar transparent in lollipop and above was creating the problem for me. Setting

在棒棒糖及以上制作状态栏是透明的,这对我来说是个问题。设置

android:fitsSystemWindows="true"
to the parent view of layout containing scrollView solved the issue.