WebView在键盘打开时无法滚动

7

我有一个包含输入表单的WebView。

当用户选择第一个输入时,键盘会出现并滚动WebView以便露出输入框。我的问题在于,在API 19以下版本中,当点击下一个部分遮挡的输入框时,WebView不会滚动。如果我关闭键盘然后再选择输入框,它就会正确地滚动。

我已经看了很多关于如果Activity是全屏模式则无法工作的内容,但我的应用程序并不是。

清单:

<?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布局:

<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>
2个回答

5

您需要添加:

android:fitsSystemWindows="true"

您需要找到片段/活动的布局文件的根元素,如下所示:

android:windowSoftInputMode="adjustResize"

把您的活动添加到清单中。


1
这个可行!终于!我花了半天时间解决这个问题。我感觉需要喝一杯来庆祝。 - Someone Somewhere

0
你需要在布局xml文件的WebView标签中添加android:windowSoftInputMode="adjustResize"

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