0

我有一個spannableable字符串和spannable sting containg圖像 和字符串,但是當我在textview上顯示該字符串圖像 與textview的基線對齊我想顯示ALIGN_TOP與 字符串我卡在這個請幫助我來整理一下。引號如何與TextView中的文本對齊?

這裏是我的代碼

 String text2 = " Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "; 

     Spannable spannable = new SpannableString(text2); 

     spannable.setSpan(new ImageSpan(mActivity, R.mipmap.start_quote_green), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     spannable.setSpan(new ImageSpan(mActivity, R.mipmap.end_quote_green), text2.length()-1, text2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

     inspirationalActivityInspirationalTextTv.setText(spannable, TextView.BufferType.SPANNABLE); 

輸出
result

要求
required

+0

提示:你濫用'mipmap'文件夾/秒。該文件夾必須包含**僅啓動器圖標**。而**沒有別的**(即:不是應用程序圖形資源)。那些必須放在'drawable'文件夾中。 –

+0

@ModularSynth我們可以將mipmap文件夾用於圖形rsource,而不是針對應用程序 –

+0

這不是問題,對。但它也不正確:[啓動圖標的res/mipmap文件夾,或其他類型圖標的res/drawable文件夾。](https://developer.android.com/studio/write/image-asset-studio .html) –

回答

2

這裏的答案

 String text2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "; 

     Spannable spannable = new SpannableString(text2); 
     spannable.setSpan(new TopImageSpan(mActivity, R.mipmap.start_quote_green), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     spannable.setSpan(new TopImageSpan(mActivity, R.mipmap.end_quote_green), text2.length()-1, text2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 


     inspirationalActivityInspirationalTextTv.setText(spannable, TextView.BufferType.SPANNABLE); 

TopImageSpan類頂部對齊文本

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.graphics.drawable.Drawable; 
import android.support.annotation.NonNull; 
import android.text.style.DynamicDrawableSpan; 
import android.text.style.ImageSpan; 

import java.lang.ref.WeakReference; 

/** 
* Created by Humayoon on 8/10/17. 
*/ 

public class TopImageSpan extends ImageSpan { 

    private WeakReference<Drawable> mDrawableRef; 
    public static boolean startQuote = false,EndQuote = true; 

    public TopImageSpan(Context context, final int drawableRes) { 
     super(context, drawableRes); 
    } 

    @Override 
    public int getSize(Paint paint, CharSequence text, 
         int start, int end, 
         Paint.FontMetricsInt fm) { 
     Drawable d = getCachedDrawable(); 
     Rect rect = d.getBounds(); 

     if (fm != null) { 
      Paint.FontMetricsInt pfm = paint.getFontMetricsInt(); 
      // keep it the same as paint's fm 
      fm.ascent = pfm.ascent; 
      fm.descent = pfm.descent; 
      fm.top = pfm.top; 
      fm.bottom = pfm.bottom; 
     } 

     return rect.right; 
    } 

    @Override 
    public void draw(@NonNull Canvas canvas, CharSequence text, 
        int start, int end, float x, 
        int top, int y, int bottom, @NonNull Paint paint) { 
     Drawable b = getCachedDrawable(); 
     canvas.save(); 

     int drawableHeight = b.getIntrinsicHeight(); 
     int fontAscent = paint.getFontMetricsInt().ascent; 
     int fontDescent = paint.getFontMetricsInt().descent; 
     int transY = bottom - b.getBounds().bottom + // align bottom to bottom 
       (drawableHeight - fontDescent + fontAscent); // align center to center 

     canvas.translate(x, transY); 
     b.draw(canvas); 
     canvas.restore(); 
    } 

    // Redefined locally because it is a private member from DynamicDrawableSpan 
    private Drawable getCachedDrawable() { 
     WeakReference<Drawable> wr = mDrawableRef; 
     Drawable d = null; 

     if (wr != null) 
      d = wr.get(); 

     if (d == null) { 
      d = getDrawable(); 
      mDrawableRef = new WeakReference<>(d); 
     } 

     return d; 
    } 
}