合理的textview支持html标签

时间:2016-07-28 05:42:51

标签: android

我正在尝试构建一个对齐的 textview,它甚至接受html格式。 当我将文本传递给自定义textview时,html标签无效,但在普通textview中工作正常

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中查看文本,根据内容我无法为相应的数据设置标记。它全部附带相同的文字样式

2 个答案:

答案 0 :(得分: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"
                />

答案 1 :(得分: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)));