首页 > 解决方案 > 如何修复在 Xamarin.Android 中始终为零的 AlertDialog(全屏)内的 WebView 中的高度?

问题描述

我一直在尝试在AlertDialog.Builder. 过了一会儿,我发现主要问题是它没有高度,因为正在加载网站:

它的外观:

错误

添加高度后的外观:

正确的

这些是我的 XML:

webview_fulscreen.xml

<?xml version="1.0" encoding="UTF-8" ?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frameFull"
    android:orientation="vertical">
    <include layout="@layout/previewer" />
    <ImageView
        android:layout_width="@dimen/close_dialog"
        android:layout_height="@dimen/close_dialog"
        android:src="@drawable/ic_close_black_48dp"
        android:clickable="true"
        android:layout_gravity="left"
        android:layout_marginTop="@dimen/close_dialog_margin_top"
        android:layout_marginLeft="@dimen/close_dialog_margin_left"
        android:tint="@color/colorLblBnd"
        android:id="@+id/previewClose"
        />
</FrameLayout>

预览器.xml

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ProgressBar
        android:id="@+id/indeterminateBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:indeterminate="true"
        android:max="100"
        android:layout_height="wrap_content"
    />
    <android.webkit.WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView" />
</LinearLayout>

这就是我调用 AlertDialog 的方式:

string videoUrl = "file:///android_asset/website/video.html";

View dialogView = LayoutInflater.From(view.Context).Inflate(Resource.Layout.webview_fullscreen, null);

var webView = dialogView.FindViewById<WebView>(Resource.Id.webView);

var imageView = dialogView.FindViewById<ImageView>(Resource.Id.previewClose);

var pBar = dialogView.FindViewById<ProgressBar>(Resource.Id.indeterminateBar);

webView.SetWebChromeClient(new WebChromeClient());

webView.Settings.JavaScriptEnabled = true;
webView.Settings.AllowFileAccessFromFileURLs = true;
webView.Settings.DomStorageEnabled = true;

webView.LoadUrl(videoUrl);

using var dialogBuilder = new AlertDialog.Builder(view.Context, Android.Resource.Style.ThemeMaterialLightNoActionBarFullscreen);

dialogBuilder.SetView(dialogView);

dialogBuilder.Show();

我尝试通过脚本设置高度,但没有成功。知道如何获得高度或计算吗?另外,我尝试以RelativeLayout编程方式设置,但它也不起作用。

标签: androidxamarin.androidandroid-webviewandroid-alertdialog

解决方案


首先,尝试在 AlertDialog 中加载一个简单的静态 html(例如,带有一些简单的标题)。

然后尝试创建一个新的xml布局,里面只有一个webview,参考https://stackoverflow.com/a/50334176/8187800

最后,如果问题仍然存在,您介意在这里分享一个基本的、最小的项目吗?


推荐阅读