Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
天和大家交流一下HtmlTextView的使用,HtmlTextView是一個開源的原生安卓第三方控件,主要的用途是直接把web端的html內(nèi)容解析后展示在安卓手機上。應(yīng)用場景很多,比如很多新聞類APP在web端編輯入庫后是html的格式,這時候如果想顯示在手機上,HtmlTextView就是一個很好的選擇,當(dāng)然有人會說谷歌自己的TextView通過一些方法也可以實現(xiàn),但是我試過,效果簡直不能再差,而且使用起來很復(fù)雜。這個HtmlTextView有多好,話不多說,直接上圖:
在使用的時候,第一步是引入控件的依賴:
repositories {
jcenter()
}
dependencies {
compile 'org.sufficientlysecure:html-textview:4.0'
}
引入依賴后有幾種不同的使用方式,根據(jù)自己實際的場景進行選擇:
大部分的html標(biāo)簽都已經(jīng)支持:
<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="...">
最后,一個最重要的問題,也是我最近使用遇到的最大的問題:
當(dāng)html里面有src或者h(yuǎn)ref等的時候,如果不做處理,APP會出現(xiàn)無法跳轉(zhuǎn)至瀏覽器并且崩潰閃退的情況。解決這個問題的辦法就是在方法里添加對應(yīng)的點擊事件,如下:
實現(xiàn)新聞詳情的時候,安卓也有類似富文本的控件,但是使用webview或者textview等方法,每種方法各有優(yōu)劣,不是很完美。今天給大家介紹一個安卓控件——HtmlTextView,實現(xiàn)詳情頁圖文混排的顯示。
1.在gradle引入控件
dependencies {
compile 'org.sufficientlysecure:html-textview:4.0'
}
123
2.在布局頁面使用上面的控件
<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" />
12345
3.在activity使用相關(guān)方法,此處是其他頁面網(wǎng)絡(luò)獲取解析傳遞過來的html內(nèi)容,HtmlHttpImageGetter方法不需要手動編寫(較textview實現(xiàn)的時候不再需要重寫、繼承ImageGetter方法,可查看上篇博客)
public class ContentActivity extends BaseActivity {
private String htmlContents = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
HtmlTextView htmlTextView = findViewById(R.id.html_text);
final Intent intent = getIntent();
Bundle bundle = intent.getExtras();
htmlContents = bundle.getCharSequence("contents").toString();
htmlTextView.setHtml(htmlContents, new HtmlHttpImageGetter(htmlTextView));
}
}
123456789101112131415
4.讓我們看一下最終實現(xiàn)的效果
有相關(guān)疑問可以留言,感謝點贊轉(zhuǎn)發(fā),禁止轉(zhuǎn)載
上圖是視圖的開發(fā)指導(dǎo),官方給了個鏈接。
國內(nèi)地址:
https://developer.android.google.cn/guide/topics/ui/index.html
view的使用
"直譯"
窗口中的所有視圖都排列在一棵樹中.
可以通過代碼或通過在一個或多個XML布局中指定視圖樹來添加視圖.
有許多專門的視圖子類可以充當(dāng)控件,或者可以顯示文本,圖像或其他內(nèi)容。
創(chuàng)建視圖樹后,通常可能需要執(zhí)行幾種類型的常見操作.
"人話"
窗口也就是windows,windows上有視圖"樹",我們在畫頁面時其實就是往視圖樹上添加view,當(dāng)然,添加的方式即可以是xml也可以是代碼的形式。下面那兩句話,舉個例子就是TextView的常見類型操作setText();
總結(jié):
我們對整個view學(xué)習(xí)要建立一個體系,有一個脈絡(luò)。(自己腦海中有一個大體的脈絡(luò),再去補充一個一個知識點)
view的基本概念->view如何使用->如何自定義view
之后我會分享我的"脈絡(luò)",希望大家多多支持.
后續(xù):
第一期:view的簡單介紹。
第二期:view添加到頁面的方式。
下一期:view的屬性和方法。
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。