将html作为字符串传递给webView

时间:2014-01-12 02:01:15

标签: android

嗨我正在尝试传入一个html字符串,只是得到一个空白屏幕我已经尝试了loadData和loadDataWithBaseUrl并且什么也没有得到任何人知道如何正确地执行此操作?

到目前为止,我试过的是什么

        final WebView webview = (WebView)this.findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);

        this.getWindow().setFeatureInt(
                Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);


        webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view,
                    String url) {

                webview.loadData(url, "text/html", "UTF-8");
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                progBar.setVisibility(View.GONE);
                loading.setVisibility(View.GONE);

            }


        });


        webview.loadDataWithBaseURL(null, editedHtml, "text/html", "UTF-8", null);

2 个答案:

答案 0 :(得分:0)

使用此

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     WebView webview = new WebView(this);
     setContentView(webview);

     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null);

}

这将创建一个webview并在html中显示“你得了192分”。确保您的清单上有互联网权限

答案 1 :(得分:0)

几乎相同:

 public class WebActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.webcontent);
 webView = (WebView) findViewById(R.id.webView);
 webView.getSettings().setJavaScriptEnabled(true);
 String customHtml = "<html><body><h2>Hello World</h2></body></html>";
 webView.loadData(customHtml, "text/html", "UTF-8");

}

}