如何以编程方式添加webview

时间:2016-11-02 10:03:19

标签: java android layout programmatically-created

当您想要以编程方式添加webview时,我遇到了问题 我尝试过这样,但是当按钮已经能够出现时,webview就无法显示 我需要一个解决这个案例的方法

    //        sizeView
        LinearLayout.LayoutParams sizeView = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );


//        viewButton
        Button home = new Button(this);
        home.setText("HOME");

        Button profile = new Button(this);
        profile.setText("PROFILE");

        Button academic = new Button(this);
        academic.setText("ACADEMIC");

        Button register = new Button(this);
        register.setText("REGISTER");

        Button about = new Button(this);
        about.setText("ABOUT");

//        viewWEB
        WebView webView = new WebView(this);


//        sideLeft
        LinearLayout sideLeft = new LinearLayout(this);
        sideLeft.setOrientation(LinearLayout.VERTICAL);
        sideLeft.setLayoutParams(sizeView);
        sideLeft.setBackgroundColor(Color.GRAY);
        sideLeft.addView(home);
        sideLeft.addView(profile);
        sideLeft.addView(academic);
        sideLeft.addView(register);
        sideLeft.addView(about);

//        wrapper
        LinearLayout layWrap = new LinearLayout(this);
        layWrap.setBackgroundColor(Color.BLUE);
        layWrap.setOrientation(LinearLayout.HORIZONTAL);
        layWrap.addView(sideLeft);
        layWrap.addView(webView);
        setContentView(layWrap);

enter image description here

2 个答案:

答案 0 :(得分:0)

您的WebView正确呈现。只需启用所需的功能,例如启用javascript,并设置chrome client和loadUrl以使其正常工作......

同时您还可以通过添加以下行来测试它:

webView.loadData("<html><body>TEST</body></html>", "text/html", "utf-8");

答案 1 :(得分:0)

    LinearLayout layWrap = new LinearLayout(this);
    layWrap.setBackgroundColor(Color.BLUE);
    layWrap.setOrientation(LinearLayout.HORIZONTAL);
    layWrap.addView(sideLeft);

    setContentView(layWrap);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
    FrameLayout.LayoutParams linearlayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
    layWrap.setLayoutParams(linearlayoutParams);

    layWrap.addView(webView);

    webView.setLayoutParams(layoutParams);
    webView.setBackgroundColor(Color.RED);
    // to enable javascripts
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setBuiltInZoomControls(true);
    // zoom if you want
    webView.getSettings().setSupportZoom(true);
    // to support url redirections
    webView.setWebViewClient(new WebViewClient());
    // extra settings
    webView.getSettings().setLoadWithOverviewMode(false);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollContainer(true);
    // setting for lollipop and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
        webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }

    webView.loadUrl("http://www.facebook.com");

完成加载webview