您可以尝试使用 EmojiCompat 库,描述为 here 和 here。将其添加到您的依赖项中;
dependencies {
...
implementation "com.android.support:support-emoji:27.1.1"
implementation "com.android.support:support-emoji-bundled:27.1.1"
...
}
初始化库;
EmojiCompat.init(new BundledEmojiCompatConfig(this).setReplaceAll(true));
然后将TextView 替换为EmojiTextView。这是一个您可以用来测试的示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText">
<!-- replace -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="TextView - default: \u270c, text: \u270c\ufe0e, emoji: \u270c\ufe0f"/>
<!-- with -->
<android.support.text.emoji.widget.EmojiTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="EmojiTextView - default: \u270c, text: \u270c\ufe0e, emoji: \u270c\ufe0f"/>
</LinearLayout>
如果它不按您的意愿工作,请从初始化行中删除 .setReplaceAll(true),然后重试,看看是否有效。
编辑:
如果您想手动绘制表情符号文本,例如对于画布,您可以使用 EmojiCompat.process(...) 和 android.text.StaticLayout 来完成。我没有尝试过,所以可能有错误,但它应该可以工作。
// assuming x, y, and paint are defined
CharSequence emoji = EmojiCompat.get().process("\u270c\ufe0e");
StaticLayout layout = new StaticLayout(emoji, paint,
canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.translate(x, y);
layout.draw(canvas);
canvas.translate(-x, -y);