Android 上的 Web 视图缩放WebView Zoom on Android

下载示例 下载示例Download Sample Download the sample

此 Android 平台特定适用于WebView上的 "缩放" 和 "缩放" 控件。This Android platform-specific enables pinch-to-zoom and a zoom control on a WebView. 它通过将 WebView.EnableZoomControlsWebView.DisplayZoomControls 可绑定属性设置为 boolean 值,在 XAML 中使用:It's consumed in XAML by setting the WebView.EnableZoomControls and WebView.DisplayZoomControls bindable properties to boolean values:

<ContentPage ...
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core">
    <WebView Source="https://www.xamarin.com"
             android:WebView.EnableZoomControls="true"
             android:WebView.DisplayZoomControls="true" />
</ContentPage>

@No__t_0 的可绑定属性控制是否在WebView上启用了 "缩小到缩放",以及 "WebView.DisplayZoomControls 可绑定" 属性控制是否在 WebView 上覆盖缩放控件。The WebView.EnableZoomControls bindable property controls whether pinch-to-zoom is enabled on the WebView, and the WebView.DisplayZoomControls bindable property controls whether zoom controls are overlaid on the WebView.

或者,可以C#使用 Fluent API 使用特定于平台的:Alternatively, the platform-specific can be consumed from C# using the fluent API:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
...

webView.On<Android>()
    .EnableZoomControls(true)
    .DisplayZoomControls(true);

@No__t_0 方法指定此平台特定的仅在 Android 上运行。The WebView.On<Android> method specifies that this platform-specific will only run on Android. @No__t_2命名空间中的 WebView.EnableZoomControls 方法用于控制是否对WebView启用 "缩小到缩放"。The WebView.EnableZoomControls method, in the Xamarin.Forms.PlatformConfiguration.AndroidSpecific namespace, is used to control whether pinch-to-zoom is enabled on the WebView. 同一命名空间中的 WebView.DisplayZoomControls 方法用于控制是否在 WebView 上重叠缩放控件。The WebView.DisplayZoomControls method, in the same namespace, is used to control whether zoom controls are overlaid on the WebView. 此外,WebView.ZoomControlsEnabledWebView.ZoomControlsDisplayed 方法可用于分别返回是否启用了 "缩小到缩放" 和 "缩放" 控件。In addition, the WebView.ZoomControlsEnabled and WebView.ZoomControlsDisplayed methods can be used to return whether pinch-to-zoom and zoom controls are enabled, respectively.

因此,可以在WebView上启用 "缩小到缩放",并在 WebView 上重叠缩放控件:The result is that pinch-to-zoom can be enabled on a WebView, and zoom controls can be overlaid on the WebView:

Android 上缩放的 Web 视图的屏幕截图Screenshot of zoomed WebView on Android

重要

缩放控件必须通过各自的可绑定属性或方法启用和显示,才能在WebView上重叠。Zoom controls must be both enabled and displayed, via the respective bindable properties or methods, to be overlaid on a WebView.