首页 > 解决方案 > 如何用 WebView 填充 AlertDialog?

问题描述

我有一个全屏 AlertDialog 在 webview 中加载一个 html 文件。我希望 html 内容填充 AlertDialog,从而填充屏幕(如果它有边框没问题,但如果没有边框会很好)。

AlertDialog 正确地填满了屏幕,但是 webview 只出现在屏幕的顶部,下面有很多空间。奇怪的是,webview 填充了宽度,但没有填充高度。看:

在此处输入图像描述

如何强制 webview 填充 AlertDialog?

这是我的代码:

WebView myMsg = new WebView(context);
myMsg.getSettings().setJavaScriptEnabled(js);
myMsg.setVerticalScrollBarEnabled(overflow);
myMsg.setHorizontalScrollBarEnabled(overflow);
myMsg.getSettings().setAllowContentAccess(true);
myMsg.getSettings().setAllowFileAccess(true);
myMsg.getSettings().setLoadWithOverviewMode(true);
myMsg.getSettings().setUseWideViewPort(true);
myMsg.getSettings().setBuiltInZoomControls(true);
myMsg.getSettings().setDisplayZoomControls(false);
myMsg.setInitialScale(1);
myMsg.setWebViewClient(new WebViewClient() {
    @Override
    // Prevents exposing the URI in API>26 !
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }   
});
myMsg.loadUrl(page);
myMsg.setBackgroundColor(0);
myMsg.setSoundEffectsEnabled(true);

AlertDialog.Builder builder = new AlertDialog.Builder(context)
        .setView(myMsg)
        .setIcon(R.drawable.ic_launcher);

final AlertDialog dialog = builder.create();

Window dialogWin = dialog.getWindow();
dialog.show();

if (dialogWin != null) {
    dialogWin.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialogWin.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    dialogWin.setFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN,
                android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

标签: androidwebviewandroid-alertdialogfullscreenfill

解决方案


推荐阅读