編輯:關於 Android 程式設計
在專案中,我們常常需要在控制項(如 Button、TextView)裡放一些符號,例如下圖這樣的箭頭按鈕:


這種需求可以用圖片來實現,但其實直接用 Unicode 碼就能顯示出來,既省流量又不必準備多套不同解析度的素材。
版面配置如下:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal" >
<Button
android:id="@+id/scroll_left"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/left_arrow" />
<Button
android:id="@+id/scroll_up"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/scroll_left"
android:text="@string/up_arrow" />
<Button
android:id="@+id/scroll_down"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_below="@id/scroll_up"
android:layout_toRightOf="@id/scroll_left"
android:text="@string/down_arrow" />
<Button
android:id="@+id/scroll_right"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/scroll_down"
android:text="@string/right_arrow" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/music" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crown" />
</LinearLayout>
在 string.xml 中,直接用 \u 加上四位十六進位碼即可:
<string name="music">\u265A</string> <string name="crown">\u266B</string> <string name="left_arrow">\u2190</string> <string name="up_arrow">\u2191</string> <string name="down_arrow">\u2193</string> <string name="right_arrow">\u2192</string>
如果是在程式碼中拼接,也可以寫成 "\u2190";而在 XML 中則建議一律使用 string 資源,方便日後統一管理與多語系維護。
以下整理一批常用符號的 Unicode 碼值,在 Android 的 string.xml 中使用 JS 欄位的數值即可(即 \uXXXX 形式)。
| 符號 | UNICODE | 符號 | UNICODE |
|---|---|---|---|
| ⇠ | \u21E0 | ⇢ | \u21E2 |
| ⇡ | \u21E1 | ⇣ | \u21E3 |
| ↞ | \u219E | ↠ | \u21A0 |
| ↟ | \u219F | ↡ | \u21A1 |
| ← | \u2190 | → | \u2192 |
| ↑ | \u2191 | ↓ | \u2193 |
| ↔ | \u2194 | ↕ | \u2195 |
| ⇄ | \u21C4 | ⇅ | \u21C5 |
| ↢ | \u21A2 | ↣ | \u21A3 |
| ⇞ | \u21DE | ⇟ | \u21DF |
| ↫ | \u21AB | ↬ | \u21AC |
| ⇜ | \u21DC | ⇝ | \u21DD |
| ↚ | \u219A | ↛ | \u219B |
| ↮ | \u21AE | ↭ | \u21AD |
| ⇦ | \u21E6 | ⇨ | \u21E8 |
| ⇧ | \u21E7 | ⇩ | \u21E9 |
| ▲ | \u25B2 | ► | \u25BA |
| ▼ | \u25BC | ◄ | \u25C4 |
| ➔ | \u2794 | ➙ | \u2799 |
| ➨ | \u27A8 | ➲ | \u27B2 |
| ➜ | \u279C | ➞ | \u279E |
| ➟ | \u279F | ➠ | \u27A0 |
| ➤ | \u27A4 | ➥ | \u27A5 |
| ➦ | \u27A6 | ➧ | \u27A7 |
| ➵ | \u27B5 | ➸ | \u27B8 |
| ➼ | \u27BC | ➽ | \u27BD |
| ➺ | \u27BA | ➳ | \u27B3 |
| ↷ | \u21B7 | ↶ | \u21B6 |
| ↻ | \u21BB | ↺ | \u21BA |
| ↵ | \u21B5 | ↯ | \u21AF |
| ➾ | \u27BE |
| 符號 | UNICODE | 符號 | UNICODE |
|---|---|---|---|
| ❤ | \u2764 | ✈ | \u2708 |
| ★ | \u2605 | ✦ | \u2726 |
| ☀ | \u2600 | ◆ | \u25C6 |
| ◇ | \u25C8 | ▣ | \u25A3 |
| ☻ | \u263B | ☺ | \u263A |
| ☹ | \u2639 | ✉ | \u2709 |
| ☎ | \u260E | ☏ | \u260F |
| ✆ | \u2706 | ☁ | \u2601 |
| ☂ | \u2602 | ❄ | \u2744 |
| ☃ | \u2603 | ❈ | \u2748 |
| ❿ | \u273F | ✀ | \u2740 |
| ❁ | \u2741 | ☘ | \u2618 |
| ❦ | \u2766 | ❑ | \u2742 |
| ☥ | \u2625 | ☮ | \u262E |
| ☯ | \u262F | ☪ | \u262A |
| ☤ | \u2624 | ✄ | \u2704 |
| ✂ | \u2702 | ☸ | \u2638 |
| ♣ | \u2693 | ☣ | \u2623 |
| ⚠ | \u26A0 | ⚡ | \u26A1 |
| ☢ | \u2622 | ♻ | \u267B |
| ♿ | \u267F | ☠ | \u2620 |
| 符號 | UNICODE | 符號 | UNICODE |
|---|---|---|---|
| $ | \u0024 | ¢ | \u00A2 |
| £ | \u00A3 | ¤ | \u00A4 |
實務上的小提醒:這類符號在不同廠商的字型中渲染效果可能有差異,箭頭與星號通常沒問題,但裝飾性符號(如 ❤、❿)在低版本或客製系統上偶爾會顯示為豆腐字。若符號是介面的關鍵元素,建議還是用 vector drawable 或 iconfont 來處理,並搭配 SpannableString 控制顏色與大小,會比直接依賴系統字型更可控。
電商 App 常見的「Hot」角標:開源 LabelView 元件實戰
先看成果,是不是跟你預期的一樣? 電商類 App 的商品圖上,幾乎都會掛一個「Hot」、「新品」、「限時」之類的角標。自己用 Canvas 畫一個三角形再加文字不算
自定義 Android 進度條:水滴形滑塊搭配漸變色的完整實作
先看效果 這個進度條有兩個視覺重點:進度末端的水滴形滑塊,以及進度條本身由綠到藍的漸變填色。下面直接上程式碼並拆解關鍵。 自定義控制項 package com.sus
Android 集成信鴿推送的完整流程與常見問題
在移動應用中,推送已經成為常見能力。常見的推送方案包括小米推送、極光推送、個推以及騰訊信鴿推送。本文以信鴿推送為例,梳理 Android 項目集成過程中的關鍵步驟與常見問
Service 入門:生命週期、呼叫順序與背景服務模擬
Service 是 Android 中常用於背景任務的元件。它可以透過 startService 啟動,也可以透過 bindService 與其他元件建立綁定。本文以簡單