Android: webview 在键盘打开时滚动不够
Android: webview doesn't scroll enough when keyboard opens
在我的应用程序中,我有一个 webview,它加载了一个用户应该执行身份验证的网页。当用户在网页中选择某个输入字段时,webview 应该:
- 专注领域
- 打开键盘
- 执行向上滚动以便用户继续看到该字段
但是,网络视图不会自动向上滚动,因此用户不会再看到该字段。如果用户尝试手动滚动,webview 只会做一个小滚动 - 不足以让用户看到他需要的一切。
问题不在于网页本身,因为当我使用 android chrome 浏览到该网页时,它会向上滚动以保持该字段在视图中并允许滚动到页面底部。我读过以下问题:
WebView doesn't scroll when keyboard opened
和
adjustPan 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。
提前致谢!
webview已经自带scrollable,不需要在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>
感谢朋友,我现在有了解决办法
在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>
activity 布局没有什么特别之处。它确实像@ramji 解释的那样在没有 ScrollView 的情况下工作。我相信它也适用于 LinearLayout 而不是 RelativeLayout,对我来说没关系。
在我的应用程序中,我有一个 webview,它加载了一个用户应该执行身份验证的网页。当用户在网页中选择某个输入字段时,webview 应该:
- 专注领域
- 打开键盘
- 执行向上滚动以便用户继续看到该字段
但是,网络视图不会自动向上滚动,因此用户不会再看到该字段。如果用户尝试手动滚动,webview 只会做一个小滚动 - 不足以让用户看到他需要的一切。 问题不在于网页本身,因为当我使用 android chrome 浏览到该网页时,它会向上滚动以保持该字段在视图中并允许滚动到页面底部。我读过以下问题: WebView doesn't scroll when keyboard opened 和 adjustPan 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。
提前致谢!
webview已经自带scrollable,不需要在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>
感谢朋友,我现在有了解决办法
在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>
activity 布局没有什么特别之处。它确实像@ramji 解释的那样在没有 ScrollView 的情况下工作。我相信它也适用于 LinearLayout 而不是 RelativeLayout,对我来说没关系。