2016-07-28 50 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)));