欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android TextView實(shí)現(xiàn)詞組高亮的示例代碼

 更新時(shí)間:2017年10月30日 11:37:28   作者:Young_Ji  
本篇文章主要介紹了Android TextView實(shí)現(xiàn)詞組高亮的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文介紹了Android TextView實(shí)現(xiàn)詞組高亮的示例代碼,分享給大家,具體如下:

HighlightTextView

Android文本高亮控件,基于View實(shí)現(xiàn)。

特點(diǎn)

  1. 文本高亮
  2. 單詞自動(dòng)換行
  3. 高亮詞組保持在同一行顯示

效果如下:

主要邏輯:

  1. 兩個(gè) Paint 負(fù)責(zé)繪制不同的文字
  2. 在每次繪制之前計(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)文章

最新評(píng)論