2011-10-10 24 views
12

Có cách nào để định dạng thẻ HTML <BlockQuote> trong chế độ xem văn bản Android không? WebViews là một tùy chọn nếu tôi có thể dễ dàng thao tác 3 cột với hình ảnh, quảng cáo và nhiều trang cuộn dọc.BlockQuotes Kiểu trong Android TextViews

Chế độ xem văn bản rõ ràng là xử lý thẻ, tôi nhận được một dòng màu xanh xấu xí để biểu thị blockquote. Có cách nào để thay đổi màu sắc của đường kẻ hoặc tốt hơn chưa cung cấp hình ảnh dòng của riêng tôi không? Cảm ơn

Trả lời

18

Nếu bạn sử dụng android.text.Html#fromHtml để tạo android.text.Spannable, của bạn sẽ được triển khai với android.text.style.QuoteSpan. Đây là QuoteSpan không cho phép cấu hình.

Giải pháp đơn giản nhất sẽ được để tìm kiếm cho tất cả các QuoteSpan s trong Spannable của bạn và thay thế chúng:

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); 
    } 
} 

với một CustomQuoteSpan lớp như:

/** 
* 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

Đây là tuyệt vời, cảm ơn! – Nick

+0

Cảm ơn, tôi sẽ thêm mẹo. Để tô màu văn bản trong khoảng trích dẫn, hãy thêm đoạn mã dưới đây vào khoảng thời gian báo giá 'spannable.setSpan (ForegroundColorSpan mới (0xFF5C5C5C), bắt đầu, kết thúc, cờ);' – deadfish

Các vấn đề liên quan