android:webview里面的对话框或弹出窗口

时间:2012-02-07 08:03:14

标签: android android-webview

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

我的网页视图保留网址WebView.loadurl()。当在对话框中添加视图时,它仍会移动到浏览器。

我去过android loading webview in dialog 但是没有怎么做的例子? 感谢

4 个答案:

答案 0 :(得分:120)

以下是示例:

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 :(得分: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 :(得分:1)

如果您尝试在弹出窗口中显示webview,则必须在弹出窗口的布局文件(popup_layout.xml)中设置线性布局的宽度和高度,如下所示。

您必须执行此操作,因为当您尝试使用' match_parent'时,弹出窗口的布局没有任何父级可以引用该尺寸。左右。

type
  MyT = object of RootObj
    str*: string

proc initMyT(str = "<initial>"): MyT =
  result.str = str

答案 3 :(得分:0)

PopupWindow代码:

@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()。希望这有助于弹出窗口。