WebView不在Dialog中加载URL

时间:2015-08-12 09:59:52

标签: android webview dialog

我正在尝试制作一个显示网页视图的Android对话框:

final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout);
WebView webView = (WebView)dialog.findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());

String customHtml = "<html><body>WebViewTest</body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
dialog.show();

但没有任何事情发生。看来webview没有大小

我被要求提供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:minHeight="300dp"
    android:padding="10sp"
    android:background="#0ff"
    android:orientation="vertical">
    <WebView
        android:background="#f0f"
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

我建议在这里查看:android: webview inside dialog or popup

而是使用AlertDialog,它将更好地满足您的需求:

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
alert.setTitle("Title here");

WebView wv = new WebView(this);
wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});

alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();
    }
});
alert.show();

答案 1 :(得分:0)

final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout);
WebView webView = (WebView)dialog.findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient());
String customHtml = "<html><body>WebViewTest</body></html>";
webView.loadData(customHtml, "text/html; charset=UTF-8", null);
dialog.show();