2016-01-09 45 views
3

我在應用自定義QuoteSpan時遇到了一個奇怪的問題。 它包括此結束標記之後的所有文本:</quote>。但是,當我嘗試將<quote>...</quote>替換爲<blockquote>...</blockquote>以跳過我的自定義HtmlTagHandler並且使用Android的QuoteSpan的默認實現時,它可以正常工作。請參見下面的截圖:Android Custom QuoteSpan問題

預期結果(使用默認QuoteSpan):http://i.stack.imgur.com/bADnU.png

電流輸出(使用我的自定義QuoteSpan):http://i.stack.imgur.com/VFpkz.png

定製HtmlTagHandler - 未處理的html標籤(用於quote標籤)

package com.demoparser.http.parser; 

import android.graphics.Typeface; 
import android.text.Editable; 
import android.text.Html; 
import android.text.Spannable; 
import android.text.Spanned; 
import android.text.style.RelativeSizeSpan; 
import android.text.style.StrikethroughSpan; 
import android.text.style.StyleSpan; 
import android.text.style.TypefaceSpan; 

import com.demoparser.util.CustomQuoteSpan; 

import org.xml.sax.XMLReader; 

public class HtmlTagHandler implements Html.TagHandler { 

    private static final float[] HEADER_SIZES = { 
      1.5f, 1.4f, 1.3f, 1.2f, 1.1f, 1f, 
    }; 

    @Override 
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { 
     if (tag.equalsIgnoreCase("del")) { 
      if (opening) { 
       start(output, new Strikethrough()); 
      } else { 
       end(output, Strikethrough.class, new StrikethroughSpan()); 
      } 
     } else if (tag.equalsIgnoreCase("pre")) { 
      if (opening) { 
       start(output, new Monospace()); 
      } else { 
       end(output, Monospace.class, new TypefaceSpan("monospace")); 
      } 
     } else if (tag.equalsIgnoreCase("quote")) { 
      if (opening) { 
       handleP(output); 
       start(output, new BlockQuote()); 
      } else { 
       handleP(output); 
       end(output, BlockQuote.class, new CustomQuoteSpan()); 
      } 
     } 
    } 

    private static void handleP(Editable text) { 
     int len = text.length(); 

     if (len >= 1 && text.charAt(len - 1) == '\n') { 
      if (len >= 2 && text.charAt(len - 2) == '\n') { 
       return; 
      } 

      text.append("\n"); 
      return; 
     } 

     if (len != 0) { 
      text.append("\n\n"); 
     } 
    } 

    private static Object getLast(Spanned text, Class kind) { 
     /* 
     * This knows that the last returned object from getSpans() 
     * will be the most recently added. 
     */ 
     Object[] objs = text.getSpans(0, text.length(), kind); 

     if (objs.length == 0) { 
      return null; 
     } else { 
      return objs[objs.length - 1]; 
     } 
    } 

    private static void start(Editable text, Object mark) { 
     int len = text.length(); 
     text.setSpan(mark, len, len, Spannable.SPAN_MARK_MARK); 
    } 

    private static void end(Editable text, Class kind, Object repl) { 
     int len = text.length(); 
     Object obj = getLast(text, kind); 
     int where = text.getSpanStart(obj); 

     text.removeSpan(obj); 

     if (where != len) { 
      text.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     } 
    } 

    public static class Strikethrough {} 
    public static class Monospace {} 
    public static class BlockQuote {} 

} 

定製QuoteSpan

package com.demoparser.util; 

/* 
* Copyright (C) 2006 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.text.Layout; 
import android.text.style.LeadingMarginSpan; 
import android.text.style.LineBackgroundSpan; 

public class CustomQuoteSpan implements LeadingMarginSpan, LineBackgroundSpan { 
    private static final int STRIPE_WIDTH = 5; 
    private static final int GAP_WIDTH = 8; 

    private final int mBackgroundColor; 
    private final int mColor; 

    public QuoteSpan() { 
     super(); 
     mBackgroundColor = 0xffddf1fd; 
     mColor = 0xff098fdf; 
    } 

    public int getLeadingMargin(boolean first) { 
     return STRIPE_WIDTH + GAP_WIDTH; 
    } 

    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 * STRIPE_WIDTH, bottom, p); 

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

    @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(mBackgroundColor); 
     c.drawRect(left, top, right, bottom, p); 
     p.setColor(paintColor); 
    } 
} 

而且我想要應用自定義QuoteSpan HTML元素:

<quote> 
<div class="q_b">  
    <div class="q_tl"> 
    <div class="q_tr"> 
    <div class="q_bl"> 
    <div class="q_br"> 
     <div class="q_body"> 
     <b class="qfont">Quote:</b> 
     <div style="padding:5px;"> 
     <div class="q_by"> 
     Originally Posted by 
     <strong>: <a href="/index.php?thread=1#msg2"> username on 1-1-2016 00:00 AM</a></strong> 
     </div> 
     Sample Quoted Text 1 
     <br> 
     <br> 
     </div> 
     </div> 
    </div> 
    </div> 
    </div> 
    </div> 
</div> 
</quote> 
<br> 
<br> 
This message should not be inside the QuoteSpan 

我使用Html.fromHtml(...)呈現跨度。

Html.fromHtml(message, null, new HtmlTagHandler()) 

這裏的日誌開的指數和關閉標籤:

START: 0 
END: 138 <-- should be 87 

任何人有任何想法,以達到相同的結果作爲默認QuoteSpan的輸出或指出它的錯誤執行?先謝謝你。

+0

請試試看:http://takeoffandroid.com/android-views/android-spannable-text-view-to-change-color-size-style-and-adding-click-event-for-特定字/ – crickpatel0024

+0

您的'QuoteSpan'工作正常,錯誤是在自定義'Html.TagHandler'內(我沒有時間查找它) – pskink

+0

對不起。我目前使用'Html.fromHtml(...)'來顯示跨度。我已經嘗試過使用'SpannableString',但有一些情況是'quote'標籤嵌套(包括其他標籤)並且需要查找它的子節點。 – jhrdcafl

回答