Android TextView實(shí)現(xiàn)詞組高亮的示例代碼
本文介紹了Android TextView實(shí)現(xiàn)詞組高亮的示例代碼,分享給大家,具體如下:
HighlightTextView
Android文本高亮控件,基于View實(shí)現(xiàn)。
特點(diǎn)
- 文本高亮
- 單詞自動(dòng)換行
- 高亮詞組保持在同一行顯示
效果如下:
主要邏輯:
- 兩個(gè) Paint 負(fù)責(zé)繪制不同的文字
- 在每次繪制之前計(jì)算將要繪制的文本是否會(huì)超出屏幕寬度,如果超出則換行
protected void onDraw(Canvas canvas) { super.onDraw(canvas); float x_draw = getPaddingLeft(); float y_draw = getPaddingTop() + dfPaint.getTextSize(); for (ExtendText t : extendTexts) { Paint paint = t.isHighlight ? hlPaint : dfPaint; float textLen = paint.measureText(t.textUnit); if (x_draw + textLen > width) { x_draw = getPaddingLeft(); y_draw += paint.getTextSize(); } canvas.drawText(t.textUnit, x_draw, y_draw, paint); x_draw += textLen; } }
Demo
Java:
public class MainActivity extends Activity { private final static String TEXT = ""; private final static String[] HIGHLIGHT = {}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); HighLightTextView hlTv = (HighLightTextView) findViewById(R.id.hlTv); hlTv.setDisplayedText(TEXT, Arrays.asList(HIGHLIGHT)); hlTv.setDefaultColor(Color.BLACK); hlTv.setHighlightColor(ContextCompat.getColor(this, R.color.colorPrimary)); } }
XML:
<com.jy.highlighttextview.HighLightTextView android:id="@+id/hlTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" app:textSize="16sp" />
Methods:
method 方法 | description 描述 |
---|---|
setDefaultColor(int color) | 設(shè)置默認(rèn)顯示顏色 |
setHighlightColor(int color) | 設(shè)置高亮顏色 |
setDisplayedText(String text, List<String> highlights) | 設(shè)置顯示的文本和高亮詞組 |
setTextSize(float size) | 設(shè)置字體大小 |
xml value:
app:defaultColor="@color/colorPrimary" app:highlightColor="@color/colorAccent" app:text="@string/app_name" app:textSize="16sp"
完整請移步github-> jiyangg -> HighlightText
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)自定義輪播圖片控件示例
我們都知道我們做軟件的時(shí)候,有些應(yīng)用是有廣告的輪番圖的,我們實(shí)現(xiàn)這個(gè)功能的時(shí)候大多數(shù)是采用:ViewPager +LinearLayout來實(shí)現(xiàn)的,今天分享一下我自己自定義的廣告輪番圖的控件!2016-11-11Android通過ksoap2傳遞復(fù)雜數(shù)據(jù)類型及CXF發(fā)布的webservice詳細(xì)介紹
這篇文章主要介紹了 Android通過ksoap2傳遞復(fù)雜數(shù)據(jù)類型詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02Dagger2 Android依賴注入學(xué)習(xí)筆記
這篇文章主要介紹了Dagger2 Android依賴注入學(xué)習(xí)筆記,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06Android開發(fā)實(shí)現(xiàn)文件關(guān)聯(lián)方法介紹
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)文件關(guān)聯(lián)方法介紹,具有一定參考價(jià)值,需要的朋友樂意了解下。2017-10-10Android中使用socket使底層和framework通信的實(shí)現(xiàn)方法
native和framework的通信是通過jni,但是這一般只是framework調(diào)用native,native如果有消息要怎樣通知上層 呢?android中GSP模塊提供一種解決思路,但是實(shí)現(xiàn)有些復(fù)雜,這里介紹一種使用socket通信的方法可以使native和framework自由通信,感興趣的朋友一起看看吧2016-11-11Android實(shí)現(xiàn)定時(shí)器的3種方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)器的3種方法,感興趣的小伙伴們可以參考一下2016-07-07Android自定義View實(shí)現(xiàn)心形圖案
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)心形圖案,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09