【发布时间】:2022-01-04 09:04:19
【问题描述】:
我的键盘出现在 Xamarin.Forms 中的 web 视图的内容上时出现问题。当我在页面底部有一个包含<input type="text" /> 的webview 并聚焦它时,键盘出现在输入上方,我无法将输入滚动到视图中。所以我无法查看我在输入中输入的内容。
该问题仅出现在应用的android版本,应用的iOS版本正确调整大小。
我搜索了互联网,发现有几个人说您应该将WindowSoftInputMode 属性设置为AdjustResize。我试过这个,没有用。其他解决方案包含许多特定于 android 的代码,对其工作与否的反应不一。这些解决方案似乎都不起作用。
我发现了一些导致问题的原因:
- 一旦我设置
global::Xamarin.Forms.Forms.SetTitleBarVisibility(this, Xamarin.Forms.AndroidTitleBarVisibility.Never);以避免显示标题栏,问题就会开始出现 - 一旦我将主页设置为
FlyOutPage(以前称为主从页面),问题就会开始出现。
这意味着如果我删除SetTitleBarVisibility 调用,并删除弹出页面,滚动工作正常。但是,尤其是第二个破坏了我的应用程序。
我创建了一个非常简单的测试项目来演示这个问题。
Android 示例(这是问题所在):
iOS 示例(开箱即用):
代码:
App.Xaml:
public partial class App : Application
{
public App()
{
InitializeComponent();
AndroidSpecific.Application.SetWindowSoftInputModeAdjust(this, AndroidSpecific.WindowSoftInputModeAdjust.Resize);
//MainPage = new MainPage();
MainPage = new FlyOut(); //this flyout is issue reason 1
}
flyout.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<FlyoutPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.FlyOut"
xmlns:pages="clr-namespace:App1">
<FlyoutPage.Flyout>
<pages:FlyOutFlyout x:Name="FlyoutPage" />
</FlyoutPage.Flyout>
<FlyoutPage.Detail>
<NavigationPage>
<x:Arguments>
<pages:MainPage></pages:MainPage>
</x:Arguments>
</NavigationPage>
</FlyoutPage.Detail>
</FlyoutPage>
MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.MainPage">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<WebView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" x:Name="webview" />
</StackLayout>
</ContentPage>
webview html 源代码:
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<div style="width:50px; height: 600px;"></div>
<input type="text" />
</body>
</html>
android mainactivity:
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
namespace App1.Droid
{
[Activity(Label = "App1", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize, WindowSoftInputMode = Android.Views.SoftInput.AdjustResize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
//this is culprit number 2:
global::Xamarin.Forms.Forms.SetTitleBarVisibility(this, Xamarin.Forms.AndroidTitleBarVisibility.Never);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
如何使 Android 版本的行为方式与 iOS 类似?这里的目标是对 xamarin 应用程序进行更改以使其在给定的 html 下正常运行,而不是更改 html 以使其在 android 中工作。
【问题讨论】:
-
你在真机上测试过吗?模拟器在某些情况下可能会出现错误,因为电脑上有物理键盘,但正在模拟软键盘。
-
您也许可以在此调整自定义渲染器 - stackoverflow.com/a/41273431/199364。
-
是的,我已经在实际手机上进行了测试。您引用的自定义渲染器链接是 ios 特定的渲染器。我的问题不在于 ios,而在于 Android
标签: xamarin.forms webview xamarin.android