【问题标题】:Android: webview doesn't scroll enough when keyboard opensAndroid:键盘打开时webview滚动不够
【发布时间】:2016-10-27 08:06:33
【问题描述】:

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

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

但是,webview 不会自动向上滚动,因此用户不再看到该字段。如果用户尝试手动滚动,webview 只会做一个小的滚动 - 不足以让用户看到他需要的一切。 问题不在于网页本身,因为当我使用 android chrome 浏览此网页时,它会向上滚动以保持该字段可见并允许滚动到页面底部。我已阅读以下问题: WebView doesn't scroll when keyboard openedadjustPan not preventing keyboard from covering EditText 但答案并没有解决问题。

我当前的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。

在我的 AndroidManifest.xml 中:

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

也试过用adjustPan代替adjustResize。

提前致谢!

【问题讨论】:

    标签: android webview scrollview


    【解决方案1】:

    多亏了一位朋友,我现在有了解决方案。

    在 AndroidManifest.xml 中:

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

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

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

    活动布局没有什么特别之处。它确实可以在没有 ScrollView 的情况下工作,就像 @ramji 解释的那样。我相信它也可以与 LinearLayout 而不是 RelativeLayout 一起使用,这对我来说并不重要。

    【讨论】:

    • 是否因为 false 声明而工作?
    • 我不知道为什么活动样式会使其工作。经过测试……确实没有用;单击表单输入会导致键盘向上滚动并覆盖 web 视图!
    • ConstraintLayoutScrollView 中顺利工作。
    【解决方案2】:

    Webview 已经自带了可滚动的,不需要在 scrollview 里面使用 将您的代码替换为:

    <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" />
    
            <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"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多