簡體   English   中英

在 Android 中不要通過 ImageSpan 在 TextView 中顯示圖像

[英]Don't show image in TextView by ImageSpan in Android

我想在TextView中將圖像顯示為笑臉。 我有一種方法可以獲取字符串並將 ImageSpans 添加到 CharSequence 中,該方法將文本表情符號(例如:-)替換為圖形版本。在此處輸入圖片說明

public Spannable addSmileySpans(CharSequence text) {
    SpannableStringBuilder builder = new SpannableStringBuilder(text);

    Matcher matcher = mPattern.matcher(text);
    while (matcher.find()) {
        int resId = mSmileyToRes.get(matcher.group());
        builder.setSpan(new ImageSpan(mContext, resId),
                        matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return builder;
}

然后我在我的適配器上使用它:

viewHolder.txtReceivedBody.setText(parser.addSmileySpans(message.body));

此外,這里定義了 TextView 元素:

<TextView
            android:id="@+id/txtReceivedBody"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/imgReceivedDirection"
            android:layout_marginRight="30dp"
            android:background="@drawable/selector_conversation_received"
            android:minHeight="40dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="4dp"
            android:paddingBottom="4dp"
            android:textSize="18sp"
            android:autoLink="all"
            android:gravity="right"/>

不幸的是,沒有在 TextView 中顯示圖像,只顯示一個主字符串。 我應該怎么做才能解決它?

提供您的模式

我的模式是 [圖像 ID]

textview.setText(addSmileySpans(context,edit_text.getText()));

public CharSequence addSmileySpans(Context context, CharSequence msg) {
SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message);
Pattern pattern = Pattern.compile("\\[([^\\[\\]]+)\\]");
if( pattern != null )
{
    Matcher matcher = pattern.matcher( your_recieved_message );
    int matchesSoFar = 0;
    while( matcher.find() )
    {
        CharSequence cs =matcher.group().subSequence(1, matcher.group().length()-1);
        int value = Integer.parseInt(cs.toString());
        System.out.println("pattern is::"+matcher.group().subSequence(1, matcher.group().length()-1)); 
        int start = matcher.start() - (matchesSoFar * 2);
        int end = matcher.end() - (matchesSoFar * 2);
        Drawable Smiley = context.getResources().getDrawable(value);
        Smiley.setBounds(0, 0,15,15);
        builder.setSpan(new ImageSpan(Smiley), start + 1, end - 1, 0 );
        builder.delete(start, start + 1);
        builder.delete(end - 2, end -1);
        matchesSoFar++;

    }
}
return builder;
}

Kotlin 版本的Manthan Patel 答案

open fun addSmileySpans(context: Context, msg: CharSequence?): CharSequence? {
        val builder = SpannableStringBuilder(msg)
        val pattern: Pattern = Pattern.compile("\\[([^\\[\\]]+)\\]")
        val matcher: Matcher = pattern.matcher(msg)
        var matchesSoFar = 0
        while (matcher.find()) {
            val cs: CharSequence = matcher.group().subSequence(1, matcher.group().length - 1)
            println("pattern is::" + matcher.group().subSequence(1, matcher.group().length - 1))
            val start: Int = matcher.start() - matchesSoFar * 2
            val end: Int = matcher.end() - matchesSoFar * 2
            val value = cs.toString()
            val resources: Resources = context.resources
            val resourceId: Int = resources.getIdentifier(value, "drawable", context.packageName)
            val drawable = ContextCompat.getDrawable(context, resourceId)
            drawable?.setBounds(0, 0, drawable.intrinsicHeight, drawable.intrinsicWidth)
            builder.setSpan(ImageSpan(drawable), start + 1, end - 1, 0)
            builder.delete(start, start + 1)
            builder.delete(end - 2, end - 1)
            matchesSoFar++
        }
        return builder
    }

用法:

創建名稱為ic_smile 的drawable res 所以現在你在R.drawable.ic_smile有 drawable 並且可以通過

val icSmile = ContextCompat.getDrawable(context,R.drawable.ic_smile)

要將其添加到文本視圖,請使用:

val text = "[ic_smile] My text"
textView.text = addSmileySpans(requireContext(),text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM