• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

SufficientlySecure/html-textview: TextView to display simple HTML

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

SufficientlySecure/html-textview

开源软件地址:

https://github.com/SufficientlySecure/html-textview

开源编程语言:

Java 94.5%

开源软件介绍:

Project stopped

This project has been stopped. 4.0 is the last release.

Feel free to fork this project and take over maintaining.

HtmlTextView for Android

HtmlTextView is an extended TextView component for Android, which can load very simple HTML by converting it into Android Spannables for viewing.

In addition to a small set of HTML tags, the library allows to load images from the local drawables folder or from the Internet.

This library is kept tiny without external dependencies.

How to import

Add this to your build.gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'org.sufficientlysecure:html-textview:4.0'
}

Example

<org.sufficientlysecure.htmltextview.HtmlTextView
            android:id="@+id/html_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="@android:style/TextAppearance.Small" />
HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);

// loads html from string and displays cat_pic.png from the app's drawable folder
htmlTextView.setHtml("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>",
    new HtmlResImageGetter(htmlTextView));

or

HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);

// loads html from string and displays cat_pic.png from the app's assets folder
htmlTextView.setHtml("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>",
    new HtmlAssetsImageGetter(htmlTextView));

or

HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);

// loads html from string and displays http://www.example.com/cat_pic.png from the Internet
htmlTextView.setHtml("<h2>Hello wold</h2><img src=\"http://www.example.com/cat_pic.png\"/>",
    new HtmlHttpImageGetter(htmlTextView));

or

HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);

// loads html from raw resource, i.e., a html file in res/raw/,
// this allows translatable resource (e.g., res/raw-de/ for german)
htmlTextView.setHtml(R.raw.help, new HtmlHttpImageGetter(htmlTextView));

or

<TextView
    android:id="@+id/html_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="@android:style/TextAppearance.Small" />
TextView htmlTextView = (TextView) view.findViewById(R.id.html_text);

// loads html from string and displays cat_pic.png from the app's drawable folder
Spanned formattedHtml = HtmlFormatter.formatHtml(new HtmlFormatterBuilder().setHtml("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>").setImageGetter(new HtmlResImageGetter(htmlTextView.getContext())));
htmlTextView.setText(formattedHtml);

Supported HTML tags

Tags supported by Android (history of Html class)

Extended support by HtmlTextView

  • <ul>
  • <ol>
  • <li>
  • <code>
  • <center>
  • <strike>

Support for HTML tables

HtmlTextView now supports HTML tables (to a limited extent) by condensing the text into a link which developers are able to render in a native WebView. To take advantage of the feature you'll need to:

  1. implement a ClickableTableSpan which provides access to the table HTML (which can be forwarded to a WebView)

  2. provide a DrawTableLinkSpan which defines what the table link should look like (i.e. text, text color, text size)

Take a look at the project's sample app for an example.

Support for A tag click listener

textView.setOnClickATagListener(new OnClickATagListener() {

    @Override
    public void onClick(View widget, @Nullable String href) {
        Toast.makeText(MainActivity.this, href, Toast.LENGTH_SHORT).show();
    }
});

We recognize the standard table tags:

  • <table>
  • <tr>
  • <th>
  • <td>

as well as the tags extended by HtmlTextView. However, support doesn’t currently extend to tags natively supported by Android (e.g. <b>, <big>, <h1>) which means tables will not include the extra styling.

Changelog

4.0

3.9

  • Add A tag click listener

3.8

  • Add HtmlFormatter for use with standard TextView

3.7

  • Fix #166
  • Migrated to AndroidX

3.6

  • Improve and fix indentions

3.5

  • Allow image compression

3.4

  • Fix textIsSelectable

3.3

  • Fix text cutting bug
  • Prevent EmptyStackException when processing malformed li tags

3.2

  • Indenting entries of ordered lists the same way as of unordered lists
  • Fix OutOfMemory

3.1

  • Override handling of <ul> <ol> and <li> tags done by newer versions of Android SDK (removes empty lines between items)

3.0

  • Removed deprecated methods and classes
  • Fix out of bounds issue

2.0

  • Introduce better API

License

Apache License v2

See LICENSE for full license text.

Authors

Contributions

Feel free to fork and do pull requests. I am more than happy to merge them. Please do not introduce external dependencies.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap