【问题标题】:How to add image in a TextView text?如何在 TextView 文本中添加图像?
【发布时间】:2013-02-27 10:48:57
【问题描述】:

我在 Google 上四处搜索,发现了一个与我类似的问题,即如何在 TextView 文本中包含图像,例如 "你好,我的名字是 [image] ",答案是这样的:

ImageSpan is = new ImageSpan(context, resId);
text.setSpan(is, index, index + strLength, 0);

我想知道这段代码,

  1. 我应该在上下文中输入什么内容或做什么?
  2. 我是否应该对text.setSpan() 做一些事情,例如导入或引用或留下文本?

如果有人能帮我分析一下,那将不胜感激。

【问题讨论】:

    标签: java android xml image textview


    【解决方案1】:

    试试这个..

        txtview.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.image, 0, 0, 0);
    

    也看到这个..http://developer.android.com/reference/android/widget/TextView.html

    在 xml 文件中试试这个

        <TextView
            android:id="@+id/txtStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableLeft="@drawable/image"
            android:drawablePadding="5dp"
            android:maxLines="1"
            android:text="@string/name"/>
    

    【讨论】:

    • 我收到一个错误“无法从 TextView 类型对非静态方法 setCompoundDrawablesWithIntrinsicBounds(int, int, int, int) 进行静态引用”
    • 感谢 Umesh,xml 方法对我有用。我为我的 TextViews 使用 xml 布局,所以我不知道这是否有区别,也许这就是它在 Java 中不起作用的原因。跨度>
    • @Umesh Lakhani:如何通过这种方法在文本中放置多个可绘制对象?
    • 在 XML 中,图像是从左侧绘制的,而不是在中心。
    • 嗨@Umesh。如何为其设置一些边距。 setCompoundDrawablePadding 什么都没做
    【解决方案2】:

    com/xyz/customandroid/ TextViewWithImages .java:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import android.content.Context;
    import android.text.Spannable;
    import android.text.style.ImageSpan;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.TextView;
    
    public class TextViewWithImages extends TextView {
    
        public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        public TextViewWithImages(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public TextViewWithImages(Context context) {
            super(context);
        }
        @Override
        public void setText(CharSequence text, BufferType type) {
            Spannable s = getTextWithImages(getContext(), text);
            super.setText(s, BufferType.SPANNABLE);
        }
    
        private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();
    
        private static boolean addImages(Context context, Spannable spannable) {
            Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E");
            boolean hasChanges = false;
    
            Matcher matcher = refImg.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;
                }
            }
            String resname = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
            int id = context.getResources().getIdentifier(resname, "drawable", context.getPackageName());
            if (set) {
                hasChanges = true;
                spannable.setSpan(  new ImageSpan(context, id),
                                    matcher.start(),
                                    matcher.end(),
                                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                                 );
            }
        }
    
            return hasChanges;
        }
        private static Spannable getTextWithImages(Context context, CharSequence text) {
            Spannable spannable = spannableFactory.newSpannable(text);
            addImages(context, spannable);
            return spannable;
        }
    }
    

    用途:

    res/layout/mylayout.xml

                <com.xyz.customandroid.TextViewWithImages
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF00"
                    android:text="@string/can_try_again"
                    android:textSize="12dip"
                    style=...
                    />
    

    请注意,如果您将 TextViewWithImages.java 放置在 com/xyz/customandroid/ 以外的其他位置,则还必须更改包名称,com.xyz.customandroid 以上。

    res/values/strings.xml

    <string name="can_try_again">Press [img src=ok16/] to accept or [img src=retry16/] to retry</string>
    

    其中 ok16.pngretry16.pngres/drawable/ 文件夹中的图标

    【讨论】:

    • 当我使用textView.setText(R.string.can_try_again); 时,它不显示图像,它只显示纯文本Press [img src=ok16/] to accept or [img src=retry16/] to retry。有什么帮助吗?这是因为我想动态加载图像并将它们设置在 textView 中。
    • @AnasAzeem 你能通过 ImageView “正常”显示 ok16 和 retry16 吗?您是否指定了 TextViewWithImages 来代替 TextView?
    • 别忘了把
    • 它可以工作,但我无法设置图像的高度宽度,也无法在文本中心获取图像
    • 它是一个很棒的类,直到您将它打包到发布版本中。它崩溃了,我仍然无法找出原因。甚至在 -keep in proguard 中标记了这个类。仍然没有运气。或者我使用的图像可能是矢量,这就是它崩溃的原因。我不知道。
    【解决方案3】:

    我尝试了许多不同的解决方案,这对我来说是最好的:

    SpannableStringBuilder ssb = new SpannableStringBuilder(" Hello world!");
    ssb.setSpan(new ImageSpan(context, R.drawable.image), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    tv_text.setText(ssb, TextView.BufferType.SPANNABLE);
    

    此代码使用最少的内存。

    【讨论】:

    • 在这种情况下添加了图像但与文本的基线对齐我想与文本顶部对齐你能帮我吗
    • 它可以工作,但是我们如何根据文本大小调整图像图标的大小
    • 又好又短!很好的答案!
    【解决方案4】:
    fun TextView.addImage(atText: String, @DrawableRes imgSrc: Int, imgWidth: Int, imgHeight: Int) {
        val ssb = SpannableStringBuilder(this.text)
    
        val drawable = ContextCompat.getDrawable(this.context, imgSrc) ?: return
        drawable.mutate()
        drawable.setBounds(0, 0,
                imgWidth,
                imgHeight)
        val start = text.indexOf(atText)
        ssb.setSpan(VerticalImageSpan(drawable), start, start + atText.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        this.setText(ssb, TextView.BufferType.SPANNABLE)
    }
    

    VerticalImageSpan 类来自很好的答案 https://stackoverflow.com/a/38788432/5381331

    使用

    val textView = findViewById<TextView>(R.id.textview)
    textView.setText("Send an [email-icon] to example@email.com.")
    textView.addImage("[email-icon]", R.drawable.ic_email,
            resources.getDimensionPixelOffset(R.dimen.dp_30),
            resources.getDimensionPixelOffset(R.dimen.dp_30))
    

    结果

    注意
    为什么VerticalImageSpan 类?
    ImageSpan.ALIGN_CENTER 属性需要 API 29。
    另外,经过测试,我看到ImageSpan.ALIGN_CENTER 仅在图像小于文本时才有效,如果图像大于文本则只有图像居中,文本不居中,它在图像底部对齐

    【讨论】:

      【解决方案5】:

      此答案基于this excellent answer by 18446744073709551615。他们的解决方案虽然很有帮助,但不会使用周围的文本调整图像图标的大小。它也不会将图标颜色设置为周围文本的颜色。

      以下解决方案采用白色方形图标,使其适合周围文本的大小和颜色。

      public class TextViewWithImages extends TextView {
      
          private static final String DRAWABLE = "drawable";
          /**
           * Regex pattern that looks for embedded images of the format: [img src=imageName/]
           */
          public static final String PATTERN = "\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E";
      
          public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
          }
      
          public TextViewWithImages(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
      
          public TextViewWithImages(Context context) {
              super(context);
          }
      
          @Override
          public void setText(CharSequence text, BufferType type) {
              final Spannable spannable = getTextWithImages(getContext(), text, getLineHeight(), getCurrentTextColor());
              super.setText(spannable, BufferType.SPANNABLE);
          }
      
          private static Spannable getTextWithImages(Context context, CharSequence text, int lineHeight, int colour) {
              final Spannable spannable = Spannable.Factory.getInstance().newSpannable(text);
              addImages(context, spannable, lineHeight, colour);
              return spannable;
          }
      
          private static boolean addImages(Context context, Spannable spannable, int lineHeight, int colour) {
              final Pattern refImg = Pattern.compile(PATTERN);
              boolean hasChanges = false;
      
              final Matcher matcher = refImg.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;
                      }
                  }
                  final String resName = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
                  final int id = context.getResources().getIdentifier(resName, DRAWABLE, context.getPackageName());
                  if (set) {
                      hasChanges = true;
                      spannable.setSpan(makeImageSpan(context, id, lineHeight, colour),
                              matcher.start(),
                              matcher.end(),
                              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                      );
                  }
              }
              return hasChanges;
          }
      
          /**
           * Create an ImageSpan for the given icon drawable. This also sets the image size and colour.
           * Works best with a white, square icon because of the colouring and resizing.
           *
           * @param context       The Android Context.
           * @param drawableResId A drawable resource Id.
           * @param size          The desired size (i.e. width and height) of the image icon in pixels.
           *                      Use the lineHeight of the TextView to make the image inline with the
           *                      surrounding text.
           * @param colour        The colour (careful: NOT a resource Id) to apply to the image.
           * @return An ImageSpan, aligned with the bottom of the text.
           */
          private static ImageSpan makeImageSpan(Context context, int drawableResId, int size, int colour) {
              final Drawable drawable = context.getResources().getDrawable(drawableResId);
              drawable.mutate();
              drawable.setColorFilter(colour, PorterDuff.Mode.MULTIPLY);
              drawable.setBounds(0, 0, size, size);
              return new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
          }
      
      }
      

      使用方法:

      只需在文本中嵌入对所需图标的引用。文本是通过textView.setText(R.string.string_resource); 以编程方式设置还是在xml 中设置都没有关系。

      要嵌入名为 example.png 的可绘制图标,请在文本中包含以下字符串:[img src=example/]

      例如,字符串资源可能如下所示:

      <string name="string_resource">This [img src=example/] is an icon.</string>
      

      【讨论】:

      • 这是一个很好的解决方案。我建议只做一个改进:在 drawable.setColorFilter 之前添加 drawable.mutate();如果不这样做,您将在应用程序的其他部分使用不同颜色的可绘制对象。
      • @moondroid 谢谢你的建议,我相应地编辑了答案。
      • 其实我有一个问题,因为我的drawable不是正方形的,这个解决方案总是让drawable的宽度和drawable的高度一样,它会不成比例地调整我的drawable的大小
      【解决方案6】:

      这部分基于earlier answer above by @A Boschman。在那个解决方案中,我发现图像的输入大小极大地影响了makeImageSpan() 正确居中对齐图像的能力。此外,我发现该解决方案通过创建不必要的行间距来影响文本间距。

      我发现BaseImageSpan(来自 Facebook 的 Fresco 库)做得特别好:

       /**
       * Create an ImageSpan for the given icon drawable. This also sets the image size. Works best
       * with a square icon because of the sizing
       *
       * @param context       The Android Context.
       * @param drawableResId A drawable resource Id.
       * @param size          The desired size (i.e. width and height) of the image icon in pixels.
       *                      Use the lineHeight of the TextView to make the image inline with the
       *                      surrounding text.
       * @return An ImageSpan, aligned with the bottom of the text.
       */
      private static BetterImageSpan makeImageSpan(Context context, int drawableResId, int size) {
          final Drawable drawable = context.getResources().getDrawable(drawableResId);
          drawable.mutate();
          drawable.setBounds(0, 0, size, size);
          return new BetterImageSpan(drawable, BetterImageSpan.ALIGN_CENTER);
      }
      

      然后像往常一样将你的 betterImageSpan 实例提供给spannable.setSpan()

      【讨论】:

        【解决方案7】:

        假设它是我们的 TextView

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawablePadding="4dp"
            android:drawableRight="@drawable/edit"
            android:text="Hello world"
            android:textSize="18dp" />
        

        现在我们可以根据我们的要求添加以下任何一行

        android:drawableLeft="@drawable/filename"
        android:drawableRight="@drawable/filename"
        android:drawableTop="@drawable/filename"
        android:drawableBottom="@drawable/filename"
        

        【讨论】:

          【解决方案8】:

          这可能对你有帮助

            SpannableStringBuilder ssBuilder;
          
                  ssBuilder = new SpannableStringBuilder(" ");
                  // working code ImageSpan image = new ImageSpan(textView.getContext(), R.drawable.image);
                  Drawable image = ContextCompat.getDrawable(textView.getContext(), R.drawable.image);
                  float scale = textView.getContext().getResources().getDisplayMetrics().density;
                  int width = (int) (12 * scale + 0.5f);
                  int height = (int) (18 * scale + 0.5f);
                  image.setBounds(0, 0, width, height);
                  ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BASELINE);
                  ssBuilder.setSpan(
                          imageSpan, // Span to add
                          0, // Start of the span (inclusive)
                          1, // End of the span (exclusive)
                          Spanned.SPAN_INCLUSIVE_EXCLUSIVE);// Do not extend the span when text add later
          
                  ssBuilder.append(" " + text);
                  ssBuilder = new SpannableStringBuilder(text);
                  textView.setText(ssBuilder);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-02
            • 2015-11-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多