【问题标题】:WebView not loading URL in DialogWebView 未在对话框中加载 URL
【发布时间】:2015-08-12 18:07:56
【问题描述】:

我正在尝试制作一个显示 web 视图的 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>

【问题讨论】:

  • 发布用于 webview 的布局。
  • 试试这个webView.loadData(customHtml, "text/html; charset=UTF-8", null);

标签: android webview dialog


【解决方案1】:

我建议看这里: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();

【讨论】:

  • @JackFinch ,如果这个答案对您有帮助,请勾选绿色标记而不是简单地说“谢谢”,这样也可以帮助未来的用户。
【解决方案2】:
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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    相关资源
    最近更新 更多