WebView doesn't scroll when keyboard opened

2019-01-28 09:11发布

问题:

I have a WebView that contains an input form. When the user selects the first input the keyboard appears and will scroll the WebView so that the input is uncovered. My problem is in < API 19 when tapping on the next partially obscured input the WebView doesn't scroll. If I were to close the keyboard and then select the input it would scroll correctly.

I've read a lot about if the Activity is FullScreen then it won't work but my app isn't.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.MyApp">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.app.MyApp.MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateVisible">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

WebView Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical">

    <WebView
        android:id="@+id/banking_webview"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/BannerOrange"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="2dp"
        android:visibility="gone">

        <ImageView
            android:id="@+id/banner_image"
            android:layout_width="64dp"
            android:layout_height="@dimen/single_line_height"
            android:scaleType="fitCenter"/>

        <TextView
            android:id="@+id/banner_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/typography_subhead"/>
    </LinearLayout>
</LinearLayout>

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:wheel="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.app.MyApp.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"/>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

回答1:

You need to add android:windowSoftInputMode="adjustResize" to your WebView tag in layout xml.



回答2:

You need to add:

android:fitsSystemWindows="true"

to you root of the layout file for the fragment/activity as well as:

android:windowSoftInputMode="adjustResize"

to you activity in the manifest.



回答3:

Check these answers:

Android webview scrolling doesn't work

Android Froyo WebView doesn't scroll vertically

Also, you can check version combatibility



回答4:

Place your WebView Layout inside a ScrollView. That will solve your problem.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical">

    <WebView
        android:id="@+id/banking_webview"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/BannerOrange"
        android:clickable="true"
        android:orientation="horizontal"
        android:padding="2dp"
        android:visibility="gone">

        <ImageView
            android:id="@+id/banner_image"
            android:layout_width="64dp"
            android:layout_height="@dimen/single_line_height"
            android:scaleType="fitCenter"/>

        <TextView
            android:id="@+id/banner_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/typography_subhead"/>
    </LinearLayout>
   </LinearLayout>
</ScrollView>