加载页面之前,AlertDialog中的WebView不能全高

时间:2019-05-15 18:52:17

标签: android android-webview android-alertdialog

AlertDialog仅在加载其中的WebView后全屏显示:

enter image description here

enter image description here

AlertDialog.Builder builder = new AlertDialog.Builder(this);

WebView wv = new WebView(this);
wv.loadUrl("https://google.com");
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        return true;
    }
});

FrameLayout container = new FrameLayout(MainActivity.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
wv.setLayoutParams(params);
container.addView(wv);
builder.setView(container);

builder.setNegativeButton(getString(R.string.close), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();
    }
});

AlertDialog dialog = builder.create();

WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(layoutParams);

dialog.show();

如何从一开始就使AlertDialog达到全高度?


编辑:我想我找到了答案;我需要这样设置WebView的高度:

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, getResources().getDisplayMetrics().heightPixels * 0.8f, getResources().getDisplayMetrics());
params.height = height;

1 个答案:

答案 0 :(得分:1)

尝试下面的代码

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);

或在您的style.xml中设置自定义样式

<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>   
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>

在放大对话框时设置此主题

alertDialog  = new AlertDialog.Builder(this, R.style.DialogTheme);