Android中實(shí)現(xiàn)詞組高亮TextView方法示例
前言
本文主要給大家介紹了關(guān)于Android實(shí)現(xiàn)詞組高亮TextView的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
HighlightTextView
Android文本高亮控件,基于View實(shí)現(xiàn)。
特點(diǎn)
- 文本高亮
- 單詞自動換行
- 高亮詞組保持在同一行顯示

主要邏輯:
兩個 Paint 負(fù)責(zé)繪制不同的文字
在每次繪制之前計(jì)算將要繪制的文本是否會超出屏幕寬度,如果超出則換行
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 (本地下載)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Android Studio實(shí)現(xiàn)簡單的通訊錄
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)簡單的通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Android?獲取手機(jī)已安裝的應(yīng)用列表實(shí)現(xiàn)詳解
這篇文章主要介紹了Android?獲取手機(jī)已安裝的應(yīng)用列表的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android原生實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼
本篇文章主要介紹了Android原生實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android基于reclyview實(shí)現(xiàn)列表回彈動畫效果
這篇文章主要為大家詳細(xì)介紹了Android基于reclyview實(shí)現(xiàn)列表回彈動畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android開發(fā)Jetpack組件WorkManager用例詳解
這篇文章主要為大家介紹了Android開發(fā)Jetpack組件WorkManager的使用案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
Android 實(shí)現(xiàn)高斯模糊效果且兼容低版本
這篇文章主要介紹了Android 實(shí)現(xiàn)高斯模糊效果且兼容低版本的相關(guān)資料,本文圖文并茂介紹的非常詳細(xì),需要的朋友可以參考下2016-09-09
Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android
本文介紹Lorinnn在開發(fā)Android過程不斷跌打滾爬中安裝的一套開發(fā)環(huán)境,相信你在使用后同樣有不錯的體會。2014-07-07

