隨著各類型應用程式的使用者對表情符號的使用量不斷迅速增加,這套標準表情符號每年都會由 Unicode 重新整理。
如果您的應用程式會顯示網際網路內容或提供文字輸入,強烈建議支援最新的表情符號字型。否則,新的表情符號可能會顯示為名為「tofu」的小正方形方塊 (☐),或其他以錯誤方式呈現的表情符號序列。
Android 11 (API 級別 30) 以下版本無法更新表情符號字型,因此在這些版本上顯示表情符號的應用程式必須手動更新。
以下是部分現代表情符號的範例。
範例 | 版本 |
---|---|
🇨🇶 | 16.0 (2024 年 9 月) |
🐦🔥 🧑🧑🧒🧒 👩🏽🦽➡️ 🇲🇶 | 15.1 (2023 年 9 月) |
🩷 🫸🏼 🐦⬛ | 15.0 (2022 年 9 月) |
🫠 🫱🏼🫲🏿 🫰🏽 | 14.0 (2021 年 9 月) |
😶🌫️ 🧔🏻♀️ 🧑🏿❤️🧑🏾 | 13.1 (2020 年 9 月) |
🥲 🥷🏿 🐻❄️ | 13.0 (2020 年 3 月) |
🧑🏻🦰 🧑🏿🦯 👩🏻🤝👩🏼 | 12.1 (2019 年 10 月) |
🦩 🦻🏿 👩🏼🤝👩🏻 | 12.0 (2019 年 2 月) |
2023 年 3 月的 BOM (Compose UI 1.4) 支援最新版本的表情符號,包括向下相容於舊版 Android (API 21 以上)。
這項支援功能不需要變更應用程式,只要使用 Text
和 TextField
(Material 2 或 Material 3),或是 BasicText
和 BasicTextField
,就能直接支援新型表情符號。
如要在應用程式中測試最新表情符號,最好的方法是在 API 30 以下的裝置上進行測試。
如要使用自訂表情符號解決方案,或因任何其他原因需要在 Compose 中停用預設表情符號解析度,可以使用 PlatformTextStyle(emojiSupportMatch)
:
Text( text = "Hello $EMOJI_TEXT", style = TextStyle( platformStyle = PlatformTextStyle( emojiSupportMatch = EmojiSupportMatch.None )/* ... */ ) )
互通性
如果應用程式在同一個 Activity
中同時使用 Views 和 Compose,請務必使用適當的 API 正確設定表情符號。下列各節說明各項 API 的使用時機。
從 ComponentActivity
延長
如果您的 Activity
是從 Compose ComponentActivity
擴充,而非 AppCompatActivity
,請按照「在不使用 AppCompat 的情況下支援表情符號」一節的說明操作。
由於您並未擴充 AppCompatActivity
,請將 Emoji2 程式庫新增至依附元件,並在檢視區塊中使用 EmojiTextView
,而非 TextView
小工具,如下列程式碼片段所示:
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val emojiTextView: EmojiTextView = findViewById(R.id.emoji_text_view) emojiTextView.text = getString(R.string.emoji_text_view, EMOJI_TEXT) val composeView: ComposeView = findViewById(R.id.compose_view) composeView.apply { setContent { // compose code } } } }
接著,在 XML 檔案中:
<androidx.emoji2.widget.EmojiTextView android:id="@+id/emoji_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" />
從 AppCompatActivity
延長
如果 Activity
擴充自 AppCompatActivity
,您可以使用 ComposeView
呼叫可組合函式。使用 Text 可組合函式時,表情符號會在各個 Android 版本中正確算繪。
如果您是從 AppCompatActivity
擴充,請從 XML 加載 TextView
,確保表情符號正確算繪。
如果您要加載 XML,請使用這種做法:
- 在
Activity
中,ComposeView
請注意下列程式碼片段中AppCompatActivity
和TextView
的用法:
class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val emojiTextView: TextView = findViewById(R.id.emoji_text_view) emojiTextView.text = getString(R.string.emoji_text_view, EMOJI_TEXT) val composeView: ComposeView = findViewById(R.id.compose_view) composeView.apply { setContent { // compose code } } } }
- 在
ComposeView
內,透過使用AndroidViewBinding
的檢視繫結,如下列程式碼片段所示:
class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView( ComposeView(this).apply { setContent { Column { Text(EMOJI_TEXT) AndroidViewBinding(ExampleViewBinding::inflate) { emojiTextView.text = EMOJI_TEXT } } } } ) } }
如要在 ComposeView
內使用 AndroidView
膨脹文字,請使用 AppCompatTextView
正確轉譯表情符號:
class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView( ComposeView(this).apply { setContent { Column { Text(EMOJI_TEXT) AndroidView( factory = { context -> AppCompatTextView(context) }, update = { it.text = EMOJI_TEXT } ) } } } ) } }
詳情請參閱「互通性 API」說明文件。
疑難排解
如果看到 tofu (☐) 而不是表情符號,請先檢查問題是否出在特定測試裝置。建議你檢查以下幾項主要事項:
- 您可能正在使用最近刷新的裝置或新的模擬器。如果可以,請嘗試使用其他常用的實體測試裝置登入 Google 帳戶。請注意,API 必須為 30 以下,才能確保表情符號在正確版本中運作。
- 測試手機不支援可下載字型。
- 檢查正確的 Google Play 服務版本。
為您推薦
- 注意:系統會在 JavaScript 關閉時顯示連結文字
- 其他考量
- Compose 中的文字
- 捲動