Android:当键盘打开时,webview 滚动不够 [英] Android: webview doesn't scroll enough when keyboard opens

查看:70
本文介绍了Android:当键盘打开时,webview 滚动不够的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个 webview,它加载一个用户应该执行身份验证的网页.当用户在网页中选择某个输入字段时,webview 应该:

In my app I have a webview which loads a webpage where the user should perform authentication. When the user selects some input field in the webpage, the webview should:

  1. 专注领域
  2. 打开键盘
  3. 向上滚动,以便用户继续看到该字段

但是,webview 不会自动向上滚动,因此用户不再看到该字段.如果用户尝试手动滚动,webview 只会做一个小的滚动——不足以让用户看到他需要的一切.问题不在于网页本身,因为当我使用 android chrome 浏览到此网页时,它确实向上滚动以保持该字段在视图中并允许滚动到页面底部.我已阅读以下问题:键盘打开时WebView不滚动adjustPan 不会阻止键盘覆盖 EditText但答案并没有解决问题.

However, the webview doesn't scroll up automatically, so the user doesn't see the field anymore. If the user tries manually to scroll, the webview does only a small scroll - not enough for the user to see everything he needs. The problem in not with the webpage itself, since when I browse to this webpage using android chrome, it does scroll up to keep the field in view and allows scrolling till the bottom of the page. I've read the following questions: WebView doesn't scroll when keyboard opened and adjustPan not preventing keyboard from covering EditText but the answers didn't fix the issue.

我当前的activity_web_viewlogin.xml:

My current activity_web_viewlogin.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/portalWebViewWrapper"
    android:orientation="vertical"
    tools:context="com.hpe.sb.mobile.app.features.login.activities.WebViewLoginActivity">
    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

   <ScrollView android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:fillViewport="true">

        <WebView
            android:id="@+id/portalWebView"
            android:layout_below="@id/app_bar"
            android:layout_width="match_parent"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

   </ScrollView>
</LinearLayout>

也试过 ScrollView 包含 LinearLayout.

Tried also that the ScrollView contains the LinearLayout.

在我的 AndroidManifest.xml 中:

In my AndroidManifest.xml:

        <activity
           android:name=".features.login.activities.WebViewLoginActivity"
           android:windowSoftInputMode="adjustResize"
           android:label="" />

还尝试了 adjustPan 而不是 adjustResize.

Tried also adjustPan instead of adjustResize.

提前致谢!

推荐答案

感谢一位朋友,我现在有了解决方案.

Thanks to a friend, I now have a solution.

在 AndroidManifest.xml 中:

In AndroidManifest.xml:

<activity
            android:name=".features.login.activities.WebViewLoginActivity"
            android:label=""
            android:windowSoftInputMode="adjustResize"
            android:theme="@style/webview"/>

在styles.xml中:

In styles.xml:

<style name="webview" parent="AppTheme.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">false</item>
    </style>

这是activity_web_viewlogin.xml:

This is activity_web_viewlogin.xml:

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

    android:id="@+id/portalWebViewWrapper"
    tools:context="com.hpe.sb.mobile.app.features.login.activities.WebViewLoginActivity">
    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />
    <WebView
        android:id="@+id/portalWebView"
        android:layout_below="@id/app_bar"
        android:layout_width="match_parent"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

活动布局没有什么特别之处.它确实可以像@ramji 解释的那样在没有 ScrollView 的情况下工作.我相信它也适用于 LinearLayout 而不是 RelativeLayout,对我来说无关紧要.

Nothing special about the activity layout. It does work without ScrollView like @ramji explained. I believe it will also work with LinearLayout instead of RelativeLayout, didn't matter to me.

这篇关于Android:当键盘打开时,webview 滚动不够的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆