我想在TextView中显示图像作为笑脸。我有一个获取字符串的方法,并将ImageSpans添加到CharSequence中,该CharSequence用图形版本替换文本表情图标,如:-)。

c7b845613857216534c9aa9319f92a11.png不要在Android中使用ImageSpan在TextView中显示图像

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 elemnt:

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和只显示图像一个主要字符串。我应该怎么做才能解决它?

+0

请乌拉圭回合后所需的图像请.. –

2014-12-06 07:00:19

+0

我只是想表明一个笑脸图像。我在上面添加了它。 –

2014-12-06 07:06:24

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐