Android ScrollView refuses to scroll to bottom

2019-04-17 22:37发布

问题:

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

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.

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.

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 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>

回答1:

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.

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:

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

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



回答3:

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:

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.

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.

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.



回答5:

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.