首页 > 解决方案 > 水平旋转设备时界面出现问题

问题描述

我的应用程序的界面分为标题(它是一个 webview)和绝对内容,其中包含第二个 webview。

在垂直方向上它可以工作并且看起来很完美。

但是当我水平转动设备时,割台停止测量 70 并测量到 30 左右.... 割台降低了高度,尽管它具有固定的高度。

为什么会这样?有什么解决办法吗?

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:PruebaHeight"
         x:Class="PruebaHeight.MainPage">
<ContentPage.Content>
    <StackLayout Spacing="0">

        <WebView x:Name="webview_header" HorizontalOptions="FillAndExpand" HeightRequest="70" BackgroundColor="Red"/>

        <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Black">
            <!-- other things -->
            <ScrollView AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Green">
                <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="0" BackgroundColor="Purple">
                    <WebView x:Name="webview" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Blue" />
                </AbsoluteLayout>
            </ScrollView>
        </AbsoluteLayout>

    </StackLayout>
</ContentPage.Content>

标签: xamlxamarinxamarin.forms

解决方案


You could try to custom a view like following code.

XamWebView.cs

public class XamWebView: WebView
{

}

Then use it in your code.

var webview= new XamWebView();

webview.HorizontalOptions = LayoutOptions.Fill;
webview.VerticalOptions = LayoutOptions.StartAndExpand;

webview.WidthRequest = 70;
webview.HeightRequest = 70;

webview.Source = "https://www.google.com/";

Creating the Custom Renderer on Android(you could get the value of heightrequest when you running)

using WebView = Android.Webkit.WebView;
[assembly: ExportRenderer (typeof(XamWebView), typeof(XamWebViewRenderer))]
namespace Core.Droid
{

public class XamWebViewRenderer : WebViewRenderer
{
    static XamWebView _xwebView = null;
    WebView _webView;

    public XamWebViewRenderer(Context context) : base(context)
    {

    }
    class XamWebViewClient : Android.Webkit.WebViewClient
    {
        public override async void OnPageFinished(WebView view, string url)
        {
            if (_xwebView != null)
            {
                int i = 10;
                while (view.ContentHeight == 0 && i-- > 0)
                    await System.Threading.Tasks.Task.Delay(100);
                _xwebView.HeightRequest = view.ContentHeight;
            }
        }
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);
        _xwebView = e.NewElement as XamWebView;
        _webView = Control;

        if (e.OldElement == null)
        {
            _webView.SetWebViewClient(new XamWebViewClient());
        }

    }
}

}


推荐阅读