Android TextView顯示html樣式的文字
先給大家說(shuō)下項(xiàng)目需求:
TextView顯示一段文字,格式為:白雪公主(姓名,字?jǐn)?shù)不確定)向您發(fā)來(lái)了2(消息個(gè)數(shù),不確定)條消息
這段文字中名字和數(shù)字的長(zhǎng)度是不確定的,還要求名字和數(shù)字各自有各自的顏色。
就想到了用
Html.fromHtml(String str)來(lái)實(shí)現(xiàn)。
看方法名很簡(jiǎn)單,就是可以顯示字符串str對(duì)應(yīng)的html格式的文本
比如:
Html.fromHtml(<font color='red' size='24'>你好</font>" )
就將你好以html格式顯示了,紅色字體 大小24
那么通過(guò)一個(gè)小Demo看下這個(gè)方法的簡(jiǎn)單使用:
我有三個(gè)字符串,字符串中姓名、數(shù)字長(zhǎng)度都是不同的,實(shí)現(xiàn)讓姓名顯示紅色,數(shù)字顯示藍(lán)色,其他文字顯示默認(rèn)灰色的效果
先寫布局文件,三個(gè)TextView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/html_text" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/html_text2" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/html_text3" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
然后Activity 的onCreate()方法
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.html_text); textView2 = (TextView) findViewById(R.id.html_text2); textView3 = (TextView) findViewById(R.id.html_text3); names = new ArrayList<>(); counts = new ArrayList<>(); message = new ArrayList<>(); names.add("奧特曼"); names.add("白雪公主與七個(gè)小矮人"); names.add("沃德天·沃納陌帥·帥德·布耀布耀德 "); counts.add(1); counts.add(123); counts.add(9090909); for (int i = 0; i < 3; i++) { message.add("<font color='red' size='20'>"+names.get(i)+"</font>"+"向您發(fā)來(lái)"+ "<font color='blue' size='30'>"+counts.get(i)+"</font>"+"條信息"); } textView.setText(Html.fromHtml(message.get(0))); textView2.setText(Html.fromHtml(message.get(1))); textView3.setText(Html.fromHtml(message.get(2))); }
看下效果圖,是不是很簡(jiǎn)單,只要簡(jiǎn)單的會(huì)html 就可實(shí)現(xiàn)這種效果
以上內(nèi)容是小編給大家分享的Android TextView顯示html樣式的文字,希望本文分享能夠幫助到大家。
- Android實(shí)現(xiàn)在TextView文字過(guò)長(zhǎng)時(shí)省略部分或滾動(dòng)顯示的方法
- android實(shí)現(xiàn)上下滾動(dòng)的TextView
- android TextView不用ScrollViewe也可以滾動(dòng)的方法
- Android中TextView實(shí)現(xiàn)垂直滾動(dòng)和上下滾動(dòng)效果
- Android TextView實(shí)現(xiàn)垂直滾動(dòng)效果的方法
- Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能【附demo源碼下載】
- Android TextView 字體滾動(dòng)效果
- Android中TextView實(shí)現(xiàn)超過(guò)固定行數(shù)顯示“...展開全部”
- Android實(shí)現(xiàn)TextView顯示HTML加圖片的方法
- Android開發(fā)實(shí)現(xiàn)TextView顯示豐富的文本
- Android DrawableTextView圖片文字居中顯示實(shí)例
- Android開發(fā)中TextView文本過(guò)長(zhǎng)滾動(dòng)顯示實(shí)現(xiàn)方法分析
相關(guān)文章
Android 開發(fā)訂單流程view實(shí)例詳解
這篇文章主要介紹了 Android 開發(fā)訂單流程view實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07android layout XML解析錯(cuò)誤的解決方法
從別的地方復(fù)制過(guò)來(lái)XML時(shí),layout預(yù)覽時(shí)提示解析錯(cuò)誤。2013-04-04Android在Sqlite3中的應(yīng)用及多線程使用數(shù)據(jù)庫(kù)的建議(實(shí)例代碼)
這篇文章主要介紹了Android在Sqlite3中的應(yīng)用及多線程使用數(shù)據(jù)庫(kù)的建議,包括編寫數(shù)據(jù)庫(kù)具體操作類、增刪改查,通過(guò)實(shí)例代碼介紹了在實(shí)際中的應(yīng)用,需要的朋友可以參考下2022-04-04AndroidStudio4.0 New Class的坑(小結(jié))
這篇文章主要介紹了AndroidStudio4.0 New Class的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07一文詳解無(wú)痕埋點(diǎn)在Android中的實(shí)現(xiàn)
很多時(shí)候因?yàn)楫a(chǎn)品都會(huì)要獲取用戶的行為,需要客戶端進(jìn)行相關(guān)的埋點(diǎn),下面這篇文章主要給大家介紹了關(guān)于無(wú)痕埋點(diǎn)在Android中實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解
這篇文章主要為大家詳細(xì)介紹了Android開源項(xiàng)目PullToRefresh下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果
這篇文章主要為大家詳細(xì)介紹了ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08