有一天收到用户的反饋,用户名帶有emoji顯示不出來。之前開發的時候確實沒考慮到emoji的問題。識別emoji,安卓官方是介紹了EmojiCompat支持庫,它能夠讓Android設備及時兼容最新的表情符號。那麼廢話不多説,怎麼使用它呢?
1.打開應用的 build.gradle 文件
2.將支持庫添加到 dependencies 部分,這會讓最終打包的apk大一些。
dependencies {
...
implementation "com.android.support:support-emoji:26.0.0"
implementation "com.android.support:support-emoji-appcompat:26.0.0"
implementation "com.android.support:support-emoji-bundled:26.0.0"
}
3.然後初始化EmojiCompat,在onCreate時調用它
private fun initEmojiCompat() {
val config: EmojiCompat.Config
config = BundledEmojiCompatConfig(context!!)
config.setReplaceAll(true)
EmojiCompat.init(config)
}
4.之後在xml使用它
<androidx.emoji.widget.EmojiAppCompatTextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:maxWidth="100dp"
android:layout_height="wrap_content"
android:text="您尚未登錄"
android:maxHeight="40dp"
android:textColor="#747781"
android:textSize="15dp"
tools:layout_width="100dp"
android:textStyle="bold"
/>
5.動態設置username
userName.text = UserManager.userInfo.name // 當name包含emoji的時候就能正常顯示了。
更具體的一下設置,可以查看官方文檔