简体   繁体   中英

Display Css Styled Html Data on ListView in Android

So, I want to display big html string that includes css (font-size, color, font-family, text-decoration etc..) part by part on a ListView . I know there are some options for this, like using WebView and TextView with fromHtml() method, but fromHtml isn't supporting css and WebView isn't divisible for ListView items. Any idea for this?

TextView very poor about css, but it isn't a tool showing a styled website content so it's enough for simple html texts. When it comes to WebView , it's a good idea for showing complex html, css, javascript contents. You can use if you need. Certainly it isn't faster then TextView , but there are some performance improvements, as follows:

// In AndroidManifest

<application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    ...
>

<activity android:name=".activity.MainActivity"
        android:hardwareAccelerated="true">
...

// In onCreate method from Activity class

WebView webView = (WebView) findViewById(R.id.webViewId);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDefaultTextEncodingName("utf-8");
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(false);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setLoadWithOverviewMode(true);
settings.setDomStorageEnabled(true);
this.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
this.setScrollbarFadingEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    this.setLayerType(View.LAYER_TYPE_HARDWARE, null); // chromium, enable hardware acceleration
else
    this.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // older android version, disable hardware acceleration

// use this, for don't getting some errors.
webView.loadDataWithBaseURL(null, "HtmlString", "text/html; charset=UTF-8", "UTF-8", null);

You can not use CSS attributes on android's native fromHtml() but surely can use third party libraries as an alternative of fromHtml() which supports CSS. One of them can be this one .

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