在页面加载之前,AlertDialog 中的 WebView 不是全高
WebView inside AlertDialog not full height until page loaded
AlertDialog 只有在其内部的 WebView 加载后才会全屏:
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;
试试下面的代码
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);
AlertDialog 只有在其内部的 WebView 加载后才会全屏:
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;
试试下面的代码
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);