Android为跨区文本添加填充

时间:2016-01-27 18:11:59

标签: java android android-layout format string-formatting

在我的应用中,我获取并解析html内容并使用Spanned显示它。 我像这样使用Spanned:

String html = "<p>Html Content</p>"
Spanned htmlSpan = Html.fromHtml(html, imageParser, null);

我尝试使用apache commons库来设置填充,但它没有用。

有没有办法设置左右填充?

编辑:忘了提,我也有html内容中的图片。我试图在TextView本身添加填充,但是这样,所有图像也都有填充。

2 个答案:

答案 0 :(得分:4)

一般来说,显示HTML的最简单方法是使用WebView。它不是最好的解决方案,但它也适用于css和javascript。

要对TextView中的跨区文字应用填充,您可以使用<blockquote>Html.fromHtml()转换为QuoteSpan。如果您不喜欢该范围的格式,可以替换它并添加自己的LeadingMarginSpan实现。通过添加TagHandler来创建和处理可跨越字符串上的自己的标记也是如此。

您可以使用以下

之类的内容搜索和替换范围
 QuoteSpan[] spans = text.getSpans(0, text.length(), QuoteSpan.class);
text.setSpan(YOUR_SPAN, text.getSpanStart(spans[0]), text.getSpanEnd(spans[0]), 0);

使用跨度的文档很少,而且教程很少,但您可以查看this以进一步阅读。

答案 1 :(得分:0)

我选择了艰难的方式。

https://developer.android.com/reference/android/text/style/QuoteSpan#quotespan

它显示Android P Developer Preview将具有以下构造函数

QuoteSpan (int color, 
                int stripeWidth, 
                int gapWidth)

多么不幸

因此,唯一的方法是实现David期望的新QuoteText。幸运的是,Android是开源的。

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/style/QuoteSpan.java

Android P源代码在哪里?我仍然无法找到它。

复制源并实现构造函数。

import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.support.annotation.ColorInt;
import android.text.Layout;
import android.text.ParcelableSpan;
import android.text.TextUtils;
import android.text.style.LeadingMarginSpan;

// https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/style/QuoteSpan.java
public class MyQuoteSpan implements LeadingMarginSpan, ParcelableSpan {
    private int mStripWidth = 2;
    private int mGapWidth = 2;
    private final int mColor;
    public MyQuoteSpan() {
        super();
        mColor = 0xff0000ff;
    }
    public MyQuoteSpan(@ColorInt int color, int stripeWidth, int gapWidth) {
        super();
        mColor = color;
        mStripWidth = stripeWidth;
        mGapWidth = gapWidth;
    }
    public MyQuoteSpan(Parcel src) {
        mColor = src.readInt();
    }
    public int getSpanTypeId() {
        return getSpanTypeIdInternal();
    }
    /** @hide */
    public int getSpanTypeIdInternal() {
        //return TextUtils.QUOTE_SPAN;
        return 9;
    }
    public int describeContents() {
        return 0;
    }
    public void writeToParcel(Parcel dest, int flags) {
        writeToParcelInternal(dest, flags);
    }
    /** @hide */
    public void writeToParcelInternal(Parcel dest, int flags) {
        dest.writeInt(mColor);
    }
    @ColorInt
    public int getColor() {
        return mColor;
    }
    public int getLeadingMargin(boolean first) {
        return mStripWidth + mGapWidth;
    }
    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
                                  int top, int baseline, int bottom,
                                  CharSequence text, int start, int end,
                                  boolean first, Layout layout) {
        Paint.Style style = p.getStyle();
        int color = p.getColor();
        p.setStyle(Paint.Style.FILL);
        p.setColor(mColor);
        c.drawRect(x, top, x + dir * mStripWidth, bottom, p);
        p.setStyle(style);
        p.setColor(color);
    }
}

最后使用它

String html = "<blockquote>Html Content</blockquote>";
Spannable contentNative;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   contentNative = (Spannable)Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT, null, null);
} else {
    contentNative = (Spannable)Html.fromHtml(content, null, null);
}
QuoteSpan[] quoteSpans = contentNative.getSpans(0, contentNative.length(), QuoteSpan.class);
int primaryColor = ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null);
int quoteStripWidth = 2;
int quoteGapWidth = 8;
for (int i = 0, len = quoteSpans.length; i < len; i++) {
    MyQuoteSpan newSpan = new MyQuoteSpan(primaryColor, quoteStripWidth, quoteGapWidth);
    int start = contentNative.getSpanStart(quoteSpans[i]);
    int end = contentNative.getSpanEnd(quoteSpans[i]);
    contentNative.removeSpan(quoteSpans[i]);
    contentNative.setSpan(newSpan, start, end, 0);
}
相关问题