2016-07-28 52 views
1

我想建立一個理由 textview甚至接受HTML格式。 當我通過文本自定義TextView的HTML標記沒有工作,但在正常的TextView其工作精細理由textview支持HTML標籤

private int mLineY; 
private int mViewWidth; 


public JustifiedTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    super.onLayout(changed, left, top, right, bottom ); 
} 

@Override 
public void setTextSize(float size) { 
    super.setTextSize(size); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    TextPaint paint = getPaint(); 
    paint.setColor(getCurrentTextColor()); 
    paint.drawableState = getDrawableState(); 
    mViewWidth = getMeasuredWidth(); 
    String text = getText().toString(); 
    mLineY = 0; 
    mLineY += getTextSize()+1; 
    Layout layout = getLayout(); 

    if (layout == null) { 
     return; 
    } 

    Paint.FontMetrics fm = paint.getFontMetrics(); 

    int textHeight = (int) (Math.ceil(fm.descent - fm.ascent)); 
    textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout.getSpacingAdd()); 

    for (int i = 0; i < layout.getLineCount(); i++) { 
     int lineStart = layout.getLineStart(i); 
     int lineEnd = layout.getLineEnd(i); 
     float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, getPaint()); 
     String line = text.substring(lineStart, lineEnd); 
     if (needScale(line) && i < layout.getLineCount() -1) { 
      drawScaledText(canvas, lineStart, line, width); 
     } else { 
      canvas.drawText(line, 0, mLineY, paint); 
     } 
     mLineY += textHeight-1; 
    } 
} 
private void drawScaledText(Canvas canvas, int lineStart, String line, float lineWidth) { 
    float x = 0; 
    if (isFirstLineOfParagraph(lineStart, line)) { 
     String blanks = " "; 
     canvas.drawText(blanks, x, mLineY, getPaint()); 
     float bw = StaticLayout.getDesiredWidth(blanks, getPaint()); 
     x += bw; 

     line = line.substring(3); 
    } 

    int gapCount = line.length() - 1; 
    int i = 0; 
    if (line.length() > 2 && line.charAt(0) == 12288 && line.charAt(1) == 12288) { 
     String substring = line.substring(0, 2); 
     float cw = StaticLayout.getDesiredWidth(substring, getPaint()); 
     canvas.drawText(substring, x , mLineY, getPaint()); 
     x += cw; 
     i += 2; 
    } 

    float d = (mViewWidth - lineWidth)/gapCount; 
    for (; i < line.length(); i++) { 
     String c = String.valueOf(line.charAt(i)); 
     float cw = StaticLayout.getDesiredWidth(c, getPaint()); 
     canvas.drawText(c, x, mLineY, getPaint()); 
     x += cw + d; 
    } 
} 

private boolean isFirstLineOfParagraph(int lineStart, String line) { 
    return line.length() > 3 && line.charAt(0) == ' ' && line.charAt(1) == ' '; 
} 

private boolean needScale(String line) { 
    if (line == null || line.length() == 0) { 
     return false; 
    } else { 
     return line.charAt(line.length() - 1) != '\n'; 
    } 
} 

} 

有沒有辦法,我可以同時適用理由以及HTML內容顯示的任何解決方案。

  "<p><b>Pregnancy complications in older women</b> <br>Some of the common risk factors are increased propensity for hypertension, gestational diabetes, preterm delivery, cardiovascular complications, risks related to multiple pregnancy and operative delivery.</p>" 

這是一個簡單的文本,我無法查看文字在我的TextView,按內容我無法設置標籤的相應數據。它的所有發貨相同的文字造型

+0

您是否嘗試過使用biubiubiu – kggoh

+0

biubiubiu?不,我從來沒有 – Geethu

+0

鏈接我https://github.com/ufo22940268/android-justifiedtextview/blob/master/justifytext-library/src/main/java/me/biubiubiu/justifytext/library/JustifyTextView.java – johnrao07

回答

0

link,我在我的代碼中使用。學分歸功於圖書館提供者。 :)

<me.biubiubiu.justifytext.library.JustifyTextView 
       android:id="@+id/jtext" 
       android:textColor="@color/chic_black" 
       android:textSize="14sp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 
0

在你的xml中使用這樣的字符串。

<string name="sample"> 
<![CDATA[<html> 
"<p><b>Pregnancy complications in older women</b> <br/>Some of the common risk factors are increased propensity for hypertension, gestational diabetes, preterm delivery, cardiovascular complications, risks related to multiple pregnancy and operative delivery.</p>" 
</html>]]> 

</string> 

而且在你的代碼中也是這樣的。大多數的html標籤都是這樣工作的,但我認爲一些標籤不被支持。

txtText.setText(Html.fromHtml(getResources().getString(R.string.sample)));