Android 警报对话框自定义视图与父级高度不匹配

Android Alert Dialog custom view not matching parent height

我正在尝试通过在警报对话框中单击按钮来打开 Web 视图。 一切正常,但我以编程方式创建的视图的高度与警报对话框不匹配。

我的警报对话框代码是:

            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(_context);
            AlertDialog alert = alertBuilder.Create();

            switch (_type)
            {
                case ButtonType.PdfButton:
                    //open pdf
                    if (_pdfUrlType)
                        alert.SetView(PdfHandler(alert));
                    break;

                default:
                    break;
            }

            alert.Show();
            alert.Window.SetLayout(700, 1800);
        }
        catch (Exception ex)
        {
        }
        finally 
        {
        }

我正在调用 pdf 处理程序,它调用 URL pdf

    private FrameLayout PdfHandler(AlertDialog dialog) 
    {
        if (_pdfUrlType)
        {
           return GenerateUrlPdf(dialog);
        }
    }

下面是生成urlpdf的代码

    private FrameLayout GenerateUrlPdf(AlertDialog dialog) 
    {

        FrameLayout layout = new FrameLayout(_context);

        FrameLayout.LayoutParams lpMatchParent = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MatchParent, FrameLayout.LayoutParams.MatchParent);
        //layout.LayoutParameters = lpMatchParent; // i applied this on frame layout but didn't work out
       // I also tried to get alertdialog height during runtime but it shows zero bcz its isn't made yet

        var loader = GenerateLoaderGif();
        var webView = GenerateWebView(loader);

        layout.AddView(webView, lpMatchParent);
        layout.AddView(loader, lpMatchParent);

        return layout;
    }

loader 和 webview 代码如下

    private WebView GenerateWebView(LinearLayout _loader) 
    {
        WebView _view = new WebView(Application.Context);
        ((WebView)_view).Settings.JavaScriptEnabled = true;
        ((WebView)_view).Settings.SetAppCacheEnabled(true);
        ((WebView)_view).Settings.SetAppCacheMaxSize(long.MaxValue);
        ((WebView)_view).Settings.AllowFileAccess = true;
        ((WebView)_view).Settings.CacheMode = CacheModes.CacheElseNetwork;
        ((WebView)_view).LoadUrl("https://docs.google.com/gview?url=" + _url);
        return _view;
    }

    private LinearLayout GenerateLoaderGif()
    {
        LinearLayout _loader = new LinearLayout(_context);
        GifImageView GifView = new GifImageView(_context);
        LinearLayout.LayoutParams loaderparams = new LinearLayout.LayoutParams(150, 150);
        loaderparams.Gravity = GravityFlags.Center;

        _loader.SetGravity(GravityFlags.Center);
        _loader.SetBackgroundColor(Color.ParseColor("#9B000000"));

        Stream input = _context.Resources.OpenRawResource(Resource.Drawable.loadingGif);

        GifView.SetBytes(Common.ConvertStreamToByte(input));
        GifView.StartAnimation();

        _loader.AddView(GifView, loaderparams);

        return _loader;
    }

当加载程序完成加载时,webview 也显示在 150 像素的有限区域中,我只将 150 像素应用于线性布局,它是 framelayout 的子布局。

在调试时我看到 webview 和 loader 的高度都是 150 像素。

下面是activity

的截图

Alertdialog setview problem

试试像下面这样改,如果有效果,你想要吗?

 private FrameLayout GenerateUrlPdf(AlertDialog dialog)
    {

        FrameLayout layout = new FrameLayout(this);

        FrameLayout.LayoutParams lpMatchParent = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MatchParent,FrameLayout.LayoutParams.MatchParent);
        //layout.LayoutParameters = lpMatchParent; // i applied this on frame layout but didn't work out
        // I also tried to get alertdialog height during runtime but it shows zero bcz its isn't made yet

        var loader = GenerateLoaderGif();
        var webView = GenerateWebView(loader);
        //webView.LayoutParameters = lpMatchParent;
        layout.AddView(webView);
        layout.AddView(loader);


        return layout;
    }
   
    private LinearLayout GenerateLoaderGif()
    {
        LinearLayout _loader = new LinearLayout(this);
        ImageView GifView = new ImageView(this);
        LinearLayout.LayoutParams loaderparams = new LinearLayout.LayoutParams(150, 150);
        loaderparams.Gravity = GravityFlags.Center;

        _loader.SetGravity(GravityFlags.Center);
        _loader.SetBackgroundColor(Color.ParseColor("#9B000000"));



        GifView.SetImageResource(Resource.Drawable.fivePlus);

        _loader.LayoutParameters = loaderparams;
        _loader.AddView(GifView);

        return _loader;
    }

效果(我只是用ImageView而不是GifImageView测试):