Android TextView實現(xiàn)詞組高亮的示例代碼
更新時間:2017年10月30日 11:37:28 作者:Young_Ji
本篇文章主要介紹了Android TextView實現(xiàn)詞組高亮的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
本文介紹了Android TextView實現(xiàn)詞組高亮的示例代碼,分享給大家,具體如下:
HighlightTextView
Android文本高亮控件,基于View實現(xiàn)。
特點
- 文本高亮
- 單詞自動換行
- 高亮詞組保持在同一行顯示
效果如下:
主要邏輯:
- 兩個 Paint 負責繪制不同的文字
- 在每次繪制之前計算將要繪制的文本是否會超出屏幕寬度,如果超出則換行
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) | 設置默認顯示顏色 |
setHighlightColor(int color) | 設置高亮顏色 |
setDisplayedText(String text, List<String> highlights) | 設置顯示的文本和高亮詞組 |
setTextSize(float size) | 設置字體大小 |
xml value:
app:defaultColor="@color/colorPrimary" app:highlightColor="@color/colorAccent" app:text="@string/app_name" app:textSize="16sp"
完整請移步github-> jiyangg -> HighlightText
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android通過ksoap2傳遞復雜數(shù)據(jù)類型及CXF發(fā)布的webservice詳細介紹
這篇文章主要介紹了 Android通過ksoap2傳遞復雜數(shù)據(jù)類型詳細介紹的相關資料,需要的朋友可以參考下2017-02-02Android開發(fā)實現(xiàn)文件關聯(lián)方法介紹
這篇文章主要介紹了Android開發(fā)實現(xiàn)文件關聯(lián)方法介紹,具有一定參考價值,需要的朋友樂意了解下。2017-10-10Android中使用socket使底層和framework通信的實現(xiàn)方法
native和framework的通信是通過jni,但是這一般只是framework調(diào)用native,native如果有消息要怎樣通知上層 呢?android中GSP模塊提供一種解決思路,但是實現(xiàn)有些復雜,這里介紹一種使用socket通信的方法可以使native和framework自由通信,感興趣的朋友一起看看吧2016-11-11