This project has been stopped. 4.0 is the last release.
Feel free to fork this project and take over maintaining.
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.
Add this to your build.gradle:
repositories {
jcenter()
}
dependencies {
compile 'org.sufficientlysecure:html-textview:4.0'
}
<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);
<p>
<div>
handled exactly like <p>
<br>
<b>
<i>
<strong>
(bug on some Android versions: generates italic)<em>
(bug on some Android versions: generates bold)<u>
<tt>
<dfn>
<sub>
<sup>
<blockquote>
<cite>
<big>
<small>
<font color="..." face="...">
<h1>
, <h2>
, <h3>
, <h4>
, <h5>
, <h6>
<a href="...">
<img src="...">
<ul>
<ol>
<li>
<code>
<center>
<strike>
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:
implement a ClickableTableSpan
which provides access to the table HTML (which can be forwarded to a WebView)
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.
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.
<ul>
<ol>
and <li>
tags done by newer versions of Android SDK (removes empty lines between items)Apache License v2
See LICENSE for full license text.
Feel free to fork and do pull requests. I am more than happy to merge them.Please do not introduce external dependencies.
TextView设置部分文字大小、加粗、倾斜、颜色、背景、分行显示。 textView如果想要强制换行的话,必须先把TextView显示方式修改为多行(android:singleLine="false"),然后才能换行。 方法一般用两种: 1、在字符串里加入“\n”,如"abc\nrc"; 2、把TextView设置为固定宽度,然后让系统自动换行。如android:layout_w
tvContent.setText(Html.fromHtml(getString(strContent))); tvContent.setMovementMethod(LinkMovementMethod.getInstance());
入职了一段时间了,每天基本上的工作是画ui,处理点击事件。背后数据的获取和显示到界面上的逻辑代码可以看也可以自己写一写不过我还没有自信能上库,所以就把前辈们写的代码加以总结,再自己实践。 一、使用自定义HtmlTextView xml中的代码如下,用了一个放在com.common.view.htmltextview包中的名为HtmlTextView的TextView: <com.commo
设置待采集的html源码,等价于setHtml($html) 用法 $html = file_get_contents('https://querylist.cc/'); $ql = QueryList::html($html); //$ql->setHtml($html); $html = $ql->getHtml();
以下指令用于将应用程序数据绑定到HTML DOM元素的属性 - Sr.No. 名称和描述 1 ng-disabled 禁用给定的控件。 2 ng-show 显示给定的控件。 3 ng-hide 隐藏一个给定的控件。 4 ng-click 表示AngularJS单击事件。 ng-disabled Directive 将ng-disabled属性添加到HTML按钮并将其传递给模型。 将模型绑定到复选框
以下示例将展示防止XSS攻击或跨站点脚本攻击。 语法 (Syntax) String safeHtml = Jsoup.clean(html, Whitelist.basic()); 哪里 Jsoup - 解析给定HTML String的主类。 html - 初始HTML字符串。 safeHtml - 清理HTML。 Whitelist - 提供默认配置以保护html的对象。 clean(
下面的示例将展示使用方法在将HTML字符串解析为Document对象后将html设置,前置或附加到dom元素。 语法 (Syntax) Document document = Jsoup.parse(html); Element div = document.getElementById("sampleDiv"); div.html("<p>This is a sample conten
下面的示例将展示在将HTML String解析为Document对象后使用方法来获取内部html和外部html。 语法 (Syntax) Document document = Jsoup.parse(html); Element link = document.select("a").first(); System.out.println("Outer HTML: " + li
目录 <script> 资料 MDN HTML 相关文档(英文 | 中文)
描述 (Description) html( val )方法获取第一个匹配元素的html内容(innerHTML)。 XML文档不提供此属性。 语法 (Syntax) 以下是使用此方法的简单语法 - <i>selector</i>.html( ) 参数 (Parameters) 以下是此方法使用的所有参数的说明 - NA 例子 (Example) 以下是一个简单的例子,简单地显示了这种方法的用法
描述 (Description) html( )方法获取第一个匹配元素的html内容。 此属性不适用于XML文档,但适用于XHTML文档。 语法 (Syntax) 以下是使用此方法的简单语法 - <i>selector</i>.html( ) 参数 (Parameters) 以下是此方法使用的所有参数的说明 - NA 例子 (Example) 以下示例将获取第一段的HTML内容并将其显示在第二段