【问题标题】:How to insert ImageView at the end of multiline TextView?如何在多行 TextView 的末尾插入 ImageView?
【发布时间】:2011-12-16 08:55:42
【问题描述】:

也许这是一个愚蠢的问题,但我找不到在 TextView 的第二行末尾插入小图像的方法。图片总是出现在整个文本视图的右侧,而不是行尾。

我想插入这样的图片:

TextTextTextTextTextTextTextTextTextText
TextTextTextText. <ImageView>

我得到的是:

TextTextTextTextTextTextTextTextTextText  <ImageView>
TextTextTextText. 

我希望有办法做到这一点。

来源:

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TESTESTESTESTESTESTESTESTESTESTESTESTES"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tv"
            android:src="@drawable/icon" />
    </RelativeLayout>

【问题讨论】:

  • 给你的xml代码,更容易看出问题。

标签: android interface textview imageview


【解决方案1】:

创建一个 ImageSpan 并将其添加到您的文本视图中。这样,您可以将图像放在文本中您想要的位置

示例 -

ImageSpan imagespan = new ImageSpan(appContext, ressource); 
text.setSpan(imagespan, i, i+ strLength, 0); //text is an object of TextView

【讨论】:

  • ImageSpan imagespan = new ImageSpan(appContext, ressource); text.setSpan(imagespan, i, i+ strLength, 0);
  • 尝试创建多个跨度并使用 TextUtils.concat() 连接它们
  • 没有TextView.setSpan方法。
  • @erdomester 是的。 OP 帖子中的评论说“文本是 TextView 的对象”......显然这不是因为那不会编译。
  • SpannableString ss = new SpannableString(title);可绘制 d = getResources().getDrawable(R.drawable.gallery_list); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); ss.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); newsTextView.setText(ss);
【解决方案2】:

完成它的最佳方法如下...

ImageGetter getter = new ImageGetter(){                 
    public Drawable getDrawable(String source){   // source is the resource name
        Drawable d = null;
        Integer id =  new Integer(0);
        id = getApplicationContext().getResources().getIdentifier(source, "drawable", "com.sampleproject");

        d = getApplicationContext().getResources().getDrawable(id);   

        if (d != null)
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        return d;
    }
};

String imgString = this.getResources().getString(R.string.text_string) + " <img src=\"img_drawable_image\"/>";
((TextView)findViewById(R.id.textview_id)).setText(
Html.fromHtml(imgString, getter, null));

【讨论】:

    【解决方案3】:

    您可以将图像添加到字符串的末尾,而无需使用下面的 imageview;

    fun addImageToEndOfTheString(text: String, drawableResourceId : Int ,context: Context) : SpannableStringBuilder {
        val drawable = ContextCompat.getDrawable(context, drawableResourceId)!!
        drawable.setBounds(14, 0, 64, 50)
        val rocketImageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
    
        val ssBuilder = SpannableStringBuilder(text)
    
        ssBuilder.setSpan(
            rocketImageSpan,
            text.length - 1,
            text.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    
        return ssBuilder
    }
    

    然后这样称呼它;

    textView.text = addImageToEndOfTheString("Your text is here", R.drawable.ic_your_image, context!!)
    

    【讨论】:

    • 我们怎样才能使图像视图可点击?我的意思是有没有办法为这张图片设置 OnClickListener?
    • stackoverflow.com/a/32063821/3341089 你可以使用 ClickableSpan 就像这个链接中的答案一样。
    【解决方案4】:
    SpannableString ss = new SpannableString(title);
    Drawable d = getResources().getDrawable(R.drawable.gallery_list);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    newsTextView.setText(ss);
    

    【讨论】:

      【解决方案5】:
      TextView textView =new TextView(this);
      SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's a smiley how are you " );
      Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.movie_add );
      ssb.setSpan( new ImageSpan( smiley ), ssb.length()-1,  ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE );  
      textView.setText( ssb, BufferType.SPANNABLE );
      

      【讨论】:

        【解决方案6】:

        如果要在文本末尾插入 Drawable,Drawable 会隐藏文本的最后一个字符,以避免在文本末尾添加另一个字符并在该字符处开始绘制。

        val myText = "Your text"    
        
        val span: Spannable = SpannableString(myText+"-")
        val android: Drawable = ContextCompat.getDrawable(this, R.drawable.yourDrawable)!!
        android.setBounds(0, 0, 30, 30)
        val image = ImageSpan(android, ImageSpan.ALIGN_BOTTOM)
        span.setSpan(image, span.indexOf("-"), span.indexOf("-")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        

        【讨论】:

          【解决方案7】:

          这可能不是最好的解决方案,但您可以尝试使用Html.fromHtml 将图像插入到您的行中。

          ImageGetter getter = new ImageGetter(){                 
              public Drawable getDrawable(String source){
                  Drawable d = null;
          
                  context.getResources().getDrawable(Integer.parseInt(source));   
          
                  if (d != null)
                      d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
          
                  return d;
              }
          };
          String imgString = <source string> + " <img src=\"R.drawable.icon\"/>";
          ((TextView)findViewById(R.id.tv)).setText(
              Html.fromHtml(imgString, getter, null));
          

          【讨论】:

            【解决方案8】:

            感谢Libin Thomas 我为RatingBar 创建了一个解决方案。我将SvgRatingBar 与 SVG 星星一起使用。

            ImageSpan 扩展了DynamicDrawableSpan,而DynamicDrawableSpan 只有一个孩子 - ImageSpan。因此,要将另一个View 附加到TextView 的末尾,我们应该得到一个由View 制成的drawable。我给SvgRatingBar添加了一个方法getDrawable()

            public class SvgRatingBar extends AppCompatRatingBar {
            
                ...
                private Drawable drawable;
            
                public Drawable getDrawable() {
                    return drawable;
                }
            
                private void init() {
                    LayerDrawable drawable = (LayerDrawable) createTile(getProgressDrawable(), false);
                    setProgressDrawable(drawable);
                    this.drawable = drawable;
                }
            

            为评分创建一个布局(layout_rating_bar.xml):

            <?xml version="1.0" encoding="utf-8"?>
            <com.example.myapplication.SvgRatingBar xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/rate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="11dp"
                android:isIndicator="true"
                android:minHeight="13dp"
                android:numStars="5"
                android:progressDrawable="@drawable/rating_bar"
                android:rating="3.5"
                android:stepSize="0.01" />
            

            然后写这段代码:

            val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val ratingBar: SvgRatingBar = inflater.inflate(R.layout.layout_rating_bar, null, false)
                as SvgRatingBar
            ratingBar.rating = 3.5f
            val drawable = ratingBar.drawable
            
            val ss = SpannableString("dsfjdskljsf dsfjsdkljfslk dsfjlksdjflsdkjf ")
            drawable.setBounds(0, 0, 170, 30) // Set width and height of the rating bar here.
            val span = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
            ss.setSpan(span, ss.length - 1, ss.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
            textView.setText(ss)
            

            【讨论】:

              【解决方案9】:

              TextView 类上的 Kotlin 扩展

              /**
               * Creates a StringSpan using the text from the TextView itself and it also adds an ImageSpan at the
               * end of the last line of the TextView.
               * This basically allows us to set a drawable at the end of the TextView text.
               */
              fun TextView.setImageSpanAtTheEnd(context: Context, @DrawableRes drawableRes: Int) {
                  text = SpannableString("$text ").apply {
                      val d = ContextCompat.getDrawable(context, drawableRes) ?: throw RuntimeException("Drawable not found!")
                      d.setBounds(0, 0, d.intrinsicWidth, d.intrinsicHeight)
                      setSpan(ImageSpan(d, ImageSpan.ALIGN_BASELINE), text.length, text.length+1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
                  }
              }
              

              你在TextView 上设置文本之后调用它就可以了。

              【讨论】:

                猜你喜欢
                • 2014-04-18
                • 1970-01-01
                • 1970-01-01
                • 2021-11-01
                • 2020-10-14
                • 1970-01-01
                • 2013-03-29
                • 2021-07-21
                • 2021-07-20
                相关资源
                最近更新 更多