如何将TextView与SpanSpable字符串中的ImageSpan垂直对齐

时间:2014-09-26 20:11:28

标签: android imagespan

大家好我的textview有点问题,我在它上面使用了一个Spannable String,完全在TextView的中间,但Text的其余部分似乎在TextView的底线上,所以如果我们说| - |是imagespan和 _ 是文本的其余部分,它是这样的:

| - | ____

我需要的是这样的事情:

| - | - - - -

这是我的代码:

    holder.theMessage.setText(getSmiledText(mContext,mCursor.getString(mCursor.getColumnIndex("message")),holder.theMessage),TextView.BufferType.SPANNABLE);

         //   holder.theMessage.setText(mCursor.getString(mCursor.getColumnIndex("message")));
           if(isright==0){
            holder.theMessage.setBackgroundResource(R.drawable.bubble_yellow);
           }else{
            holder.theMessage.setBackgroundResource(R.drawable.bubble_green);
           }

           holder.theMessage.setGravity(Gravity.CENTER_VERTICAL);


 public static Spannable getSmiledText(Context context, CharSequence text,TextView tv) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addSmiles(context, spannable,tv);   
        return spannable;
        }



 public static boolean addSmiles(Context context, Spannable spannable,final TextView tv) {
        boolean hasChanges = false;
        for (Entry<Pattern,String> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);

        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(),
                    matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;
                    break;
                }
            if (set) {

                if(sources.get(entry.getValue()).equals("internal")){
                Drawable mDrawable = null;
                try {
                    mDrawable = new BitmapDrawable(context.getResources(),context.getResources().getAssets().open(entry.getValue()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if(types.get(entry.getValue()) == true){
                mDrawable.setBounds(0, 0, getDimensions.MAIN_BUTTON_TEXT_SIZE*4,  getDimensions.MAIN_BUTTON_TEXT_SIZE*4);

                }else{

                int width = mDrawable.getIntrinsicWidth();
                int height = mDrawable.getIntrinsicHeight();
                mDrawable.setBounds(0, 0, width > 0 ? width : 0, height > 0 ? height : 0);  

                }

                hasChanges = true;

                spannable.setSpan(new ImageSpan(mDrawable,ImageSpan.ALIGN_BASELINE),matcher.start(), matcher.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }

如果你知道解决方案,请帮助我解决它,我已经尝试了所有的重力但是它太奇怪了为什么图像在中间,但其余的文字在底部!

0 个答案:

没有答案