2011-10-10 109 views
12

有沒有在Android textview中格式化<BlockQuote>HTML標記的方法?如果我可以通過圖像,廣告和多個垂直滾動頁面輕鬆操作3列,則WebView是一個選項。Android TextViews中的樣式塊引用

文本視圖顯然處理標記,我得到一個醜陋的藍線表示塊引用。有沒有辦法改變線條的顏色或更好地提供我自己的線條圖像?由於

回答

18

如果使用android.text.Html#fromHtml建立你android.text.Spannable,您將與android.text.style.QuoteSpan實現。正是這個QuoteSpan不允許配置。

最簡單的解決辦法是在你的Spannable搜索所有QuoteSpan S和替換它們:

private void replaceQuoteSpans(Spannable spannable) { 
    QuoteSpan[] quoteSpans = spannable.getSpans(0, spanned.length(), QuoteSpan.class); 
    for (QuoteSpan quoteSpan : quoteSpans) { 
     int start = spannable.getSpanStart(quoteSpan); 
     int end = spannable.getSpanEnd(quoteSpan); 
     int flags = spannable.getSpanFlags(quoteSpan); 
     spannable.removeSpan(quoteSpan); 
     spannable.setSpan(new CustomQuoteSpan(
       MY_BACKGROUND_COLOR, 
       MY_STRIPE_COLOR, 
       MY_STRIPE_WIDTH, 
       MY_GAP_WIDTH), 
      start, 
      end, 
      flags); 
    } 
} 

CustomQuoteSpan類,如:

/** 
* android.text.style.QuoteSpan hard-codes the strip color and gap. :(
*/ 
public class CustomQuoteSpan implements LeadingMarginSpan, LineBackgroundSpan { 
    private final int backgroundColor; 
    private final int stripeColor; 
    private final float stripeWidth; 
    private final float gap; 

    public CustomQuoteSpan(int backgroundColor, int stripeColor, float stripeWidth, float gap) { 
     this.backgroundColor = backgroundColor; 
     this.stripeColor = stripeColor; 
     this.stripeWidth = stripeWidth; 
     this.gap = gap; 
    } 

    @Override 
    public int getLeadingMargin(boolean first) { 
     return (int) (stripeWidth + gap); 
    } 

    @Override 
    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 paintColor = p.getColor(); 

     p.setStyle(Paint.Style.FILL); 
     p.setColor(stripeColor); 

     c.drawRect(x, top, x + dir * stripeWidth, bottom, p); 

     p.setStyle(style); 
     p.setColor(paintColor); 
    } 

    @Override 
    public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) { 
     int paintColor = p.getColor(); 
     p.setColor(backgroundColor); 
     c.drawRect(left, top, right, bottom, p); 
     p.setColor(paintColor); 
    } 
} 
+0

這是真棒,謝謝! – Nick

+0

謝謝,我會加小費。要在報價範圍內給文本添加顏色,請在下面添加代碼belose setting quote span'spannable.setSpan(new ForegroundColorSpan(0xFF5C5C5C),start,end,flags);' – deadfish