2014-09-30 71 views
4

按照這個question,我可以在圖像周圍顯示文字。但是,我有以下問題。圍繞圖像錯誤的Android文字

enter image description here

正如你所看到的,對於頂部的圖像的空間顯示在正確的每個段落。在這個問題有人有這個問題,並建議改變'ss.length()''線'。這似乎工作,除非第一段太短,下一段會重疊圖像。

我稍微修改了FlowTextHelper類以使用Html中的文本。這是我正在使用的代碼:

public class FlowTextHelper { 
    private static boolean mNewClassAvailable; 

    /* class initialization fails when this throws an exception */ 
    static { 
     try { 
      Class.forName("android.text.style.LeadingMarginSpan$LeadingMarginSpan2"); 
      mNewClassAvailable = true; 
     } catch (Exception ex) { 
      mNewClassAvailable = false; 
     } 
    } 

    public static void tryFlowText(String text, View thumbnailView, TextView messageView, Display display, int addPadding){ 
     // There is nothing I can do for older versions, so just return 
     if(!mNewClassAvailable) return; 

     // Get height and width of the image and height of the text line 
     thumbnailView.measure(display.getWidth(), display.getHeight()); 
     int height = thumbnailView.getMeasuredHeight(); 
     int width = thumbnailView.getMeasuredWidth() + addPadding; 
     messageView.measure(width, height); //to allow getTotalPaddingTop 
     int padding = messageView.getTotalPaddingTop(); 
     float textLineHeight = messageView.getPaint().getTextSize(); 

     // Set the span according to the number of lines and width of the image 
     int lines = (int)Math.round((height - padding)/textLineHeight); 
     //SpannableString ss = new SpannableString(text); 
     //For an html text you can use this line: 
     if(!text.equals("")) { 
      SpannableStringBuilder ss = (SpannableStringBuilder) Html.fromHtml(text); 
      ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(), 0); 
      messageView.setText(ss); 
      messageView.setMovementMethod(LinkMovementMethod.getInstance()); // links 

      // Align the text with the image by removing the rule that the text is to the right of the image 
      RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) messageView.getLayoutParams(); 
      int[] rules = params.getRules(); 
      rules[RelativeLayout.RIGHT_OF] = 0; 
     } 
    } 
} 

public class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 { 
    private int margin; 
    private int lines; 

    public MyLeadingMarginSpan2(int lines, int margin) { 
     this.margin = margin; 
     this.lines = lines; 
    } 

    @Override 
    public int getLeadingMargin(boolean first) { 
     return first ? margin : 0; 
    } 

    @Override 
    public int getLeadingMarginLineCount() { 
     return lines; 
    } 

    @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) {} 
} 

什麼導致空間重複每一段落,我該如何擺脫它?任何幫助表示讚賞。

+0

大家很奇怪,爲什麼這種行爲發生,這是在android系統中的錯誤:https://code.google.com/p/ android/issues/detail?id = 38003 – Parker 2016-04-01 03:58:37

回答

4

我已經花時間來解決這個問題,但由於答案在這裏找到解決它: text wrapping around image in android

基本上如下:

加上一個保證金您的TextView和設置文本

final RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams)messageView.getLayoutParams(); 
params.setMargins(marginWidth, 0, 0, 0); 
messageView.setText(Html.fromHtml(text)); 

然後添加一個OnGlobalLayoutListener,並在onGlobalLayout()調用中計算實際需要多少行的邊距。您在2個獨立的spannables分割線和保證金僅添加到第一個:

 messageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

      @SuppressLint("NewApi") 
      @SuppressWarnings("deprecation") 
      @Override 
      public void onGlobalLayout() { 

       int linesCount = messageView.getLayout().getLineCount(); 
       // restore the margin 
       params.setMargins(0, 0, 0, 0); 
       SpannableString spanS = new SpannableString (Html.fromHtml(text)); 

       if (linesCount <= lines) { 
        spanS.setSpan(new MyLeadingMarginSpan2(lines, width), 0, spanS.length(), 0); 
        messageView.setText(spanS); 
       } else { 
        // find the breakpoint where to break the String. 
        int breakpoint = messageView.getLayout().getLineEnd(lines-1); 

        Spannable s1 = new SpannableStringBuilder(spanS, 0, breakpoint); 
        s1.setSpan(new MyLeadingMarginSpan2(lines, width), 0, s1.length(), 0); 
        Spannable s2 = new SpannableStringBuilder(System.getProperty("line.separator")); 
        Spannable s3 = new SpannableStringBuilder(spanS, breakpoint, spanS.length()); 
        // It is needed to set a zero-margin span on for the text under the image to prevent the space on the right! 
        s3.setSpan(new MyLeadingMarginSpan2(0, 0), 0, s3.length(), 0); 
        messageView.setText(TextUtils.concat(s1, s2, s3)); 
       } 

       // remove the GlobalLayoutListener 
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
        messageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);       
       } else { 
        messageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       } 
      } 
     }); 
+0

看起來不錯。如果我有機會回到這個問題,我會嘗試一下。 – apSTRK 2015-01-16 03:30:58

+1

什麼是marginWidth? – Umesh 2015-05-29 06:40:13

+0

嗨,你在哪裏添加OnGlobalLayoutListener?它在FlowTextHelper中嗎? – 2015-06-02 15:51:07

0

如果您需要在圖像周圍環繞文字,請使用此庫FlowTextView

該庫運行良好,可與多條線路配合使用。但是,它不支持字體的屏幕像素大小。我找到了此answer的解決方法,以便您可以將像素大小轉換爲sp。

我希望這可以幫助任何人,而且不會浪費我使用原始帖子中問題的時間。