Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發實用:常用特殊字元(箭頭、符號等)的 Unicode 碼值對照

編輯:關於 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 對照表

以下整理一批常用符號的 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 控制顏色與大小,會比直接依賴系統字型更可控。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved