Android:弹出窗口或弹出框内的WebView

57

如何在对话框或弹出窗口中添加 Web View。

我的 Web View 保存了 URL WebView.loadurl()。但是当将视图添加到对话框内时,它仍然会跳转到浏览器。

我曾经看过 android loading webview in dialog,但没有提供具体的操作示例。谢谢。

4个回答

136

这是一个例子:

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();

谢谢,现在使用AlertDialog确实有效了。但为什么PopupWindow还不起作用呢?- user1194088 刚刚 - roy.d
如果我想在页面加载时显示一个加载指示器怎么办? - user1940676
我想知道如何将重力设置为中心,因为默认情况下它是向左的。 - CompEng
尝试显示谷歌云端共享文档时出现问题,无法正常工作。 - Jorny
我曾尝试使用对话框片段,但它并没有起作用。上述解决方案有效,但如果有人知道如何在对话框片段中实现WebView,请告诉我。(在我的情况下,大多数时候无法在对话框片段中显示WebView内容)。 - brijexecon
嗨。在AlertBuilder创建的对话框弹出窗口上似乎无法工作。 - HX_unbanned

4
您需要重写setWebViewClient()函数。
mWebView = (WebView) view.findViewById(R.id.wv1);
mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});
mWebView.loadUrl(mUrl);

2
如果您想在弹出窗口中显示Webview,则必须在弹出窗口的布局文件(popup_layout.xml)中设置线性布局的宽度和高度,如下所示。您必须这样做,因为弹出窗口的布局没有任何父级可供参考大小,当您尝试使用'match_parent'等时会出现问题。
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="400dp"
android:layout_height="400dp"
android:background="#FAFAFA"
android:id="@+id/popup_layout"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/txtclose"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="end"
    android:background="@color/colorPrimaryDark"
    android:text="X"
    android:textColor="@color/main_yellow"
    android:textStyle="bold" />

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/externalUrl"/>
</LinearLayout>

0

弹出窗口代码:

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    try 
    { 
        int[] location = new int[2]; 

        (xml item where you want it to appear).getLocationOnScreen(location);
        //Get x and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
private void popTab(final Activity myActivity) 
{
    popupWidth = 350;
    popupHeight = 600;

    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);

    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) myActivity.findViewById(R.id.myMainLayoutID);
    LayoutInflater layoutInflater = (LayoutInflater) myActivity
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layoutTab = layoutInflater.inflate(R.layout.mylayout, viewGroup);

    //Get webview from xml
    WebView wv = (WebView)layoutTab.findViewById(R.id.webView2);

    // Creating the PopupWindow
    final PopupWindow popup = new PopupWindow(layoutTab);
        //Set to view
    popup.setContentView(layoutTab);

        //Setup webview
    wv.loadUrl("http:\\www.google.com");
    wv.setWebViewClient(new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);

            return true;
        }
    });

    //Add some animation from style folder
    popup.update();
    popup.setAnimationStyle(R.style.Animation);
    popup.setFocusable(true);

    popup.showAtLocation(layoutTab, Gravity.NO_GRAVITY, p.x, p.y);
}

在任何需要的地方使用popTab()。希望这对弹出窗口有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接