有一天收到用戶的反饋,用戶名帶有emoji顯示不出來。以前開發的時候確實沒考慮到emoji的問題。識別emoji,安卓官方是介紹了EmojiCompat支持庫,它可以讓Android設備及時兼容最新的表情符號。那麼廢話很少說,怎麼使用它呢?
1.打開應用的 build.gradle 文件
2.將支持庫添加到 dependencies 部分,這會讓最終打包的apk大一些。android
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時調用它app
private fun initEmojiCompat() { val config: EmojiCompat.Config config = BundledEmojiCompatConfig(context!!) config.setReplaceAll(true) EmojiCompat.init(config) }
4.以後在xml使用它ide
<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.動態設置usernamegradle
userName.text = UserManager.userInfo.name // 當name包含emoji的時候就能正常顯示了。
更具體的一下設置,能夠查看官方文檔ui