在 Android 中显示表情符号

人气:943 发布:2022-10-16 标签: dynamic android imageview emoticons

问题描述

我的 IM 应用程序必须支持表情符号.它们是 GIF 并具有文本表示,如果用户选择其中之一,则会在输入框中使用.但我想在它们发送后将它们显示为图像.目前我的自定义数组适配器在一行的 TextView 中显示发送的消息.

My IM app has to support emoticons. They are GIFs and have textual representations, which are used in the input box if the user selects one of them. But I'd like to display them as images after they have been sent.Currently my custom array adapter displays the sent message in a TextView of a row.

根据文本表示的出现动态显示图像的正确方法是什么?我是否必须搜索表情符号文本,如果找到,请从布局中删除 TextView(relativeLayout 最适合?)并添加一个带有 IM 开头的 TextView、一个带有表情符号的 ImageView 和另一个 TextView.如果同时发送更多表情符号可能会很混乱.

What is the proper method to display images dynamically based on the occurrence of their textual representation? Do I have to search for emoticon texts, and if one found, remove the TextView from the layout (relativeLayout fits most?) and add a TextView with the beginning of the IM, an ImageView with the emoticon and another TextView. If more emoticons sent simultaneously it can be messy.

有没有更简单更合乎逻辑的方法?

Is there an easier and more logical way?

推荐答案

我会尝试使用正则表达式将每个表情符号的所有出现替换为 <img> 标记.然后,将该 HTML 转换为 SpannedString 通过 Html.fromHtml().SpannedString 可以在 TextView 上的 setText() 调用中使用.

I would try using a regular expression to replace all occurrences of each emoticon with an <img> tag. Then, convert that HTML into a SpannedString via Html.fromHtml(). That SpannedString can be used in a setText() call on TextView.

141