简体   繁体   中英

Android webview - webpage renders in a very broken way

I am trying to load this popular podcast in an Android WebView:

http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs

And this is how I render it:

public class PodcastsActivity extends BaseActivity  
{    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        WebView webview = new WebView(this);
        webview.getSettings().setAppCacheEnabled(false);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setInitialScale(100);
        webview.setWebViewClient(new WebViewClient());

        setContentView(webview);
        webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");           

And in my manifest I have the activity defined like this:

    <activity
        android:name="PodcastsActivity"
        android:label="Podcasts" 
        android:hardwareAccelerated="true"/> 

But it renders like this:

在此处输入图片说明

Is there an extra setting that needs to be set? Or something I am missing?

Thanks!

I hope you have looked at other answers. Some of the things you may try are:

1) From webview not loading correctly in application question, you may try and enable java script before loading the URL:

webview.setInitialScale(1);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);

2) As per Android webview not rendering html content correctly question, if your target is higher than 2.3.3 try adding this in your manifest file.

android:hardwareAccelerated="true"

Update

3) Also check that you have following permission in manifest as direct child to <manifest> tag:

<uses-permission android:name="android.permission.INTERNET" />

Unable to load webpage using WebView in Android

4) You can try shouldOverrideUrlLoading() method as stated here .

Update 2 :

As you said that it displays correctly on your browser, there is another possibility of using the above method in case when a certain link is clicked within the app, it opens the default browser. I'm not sure if you would want this but it is a possibility. Something as follows:

webview.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
});

webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");

Source: WebView link click open default browser

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM