處理文字
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
對於數位時鐘,您應盡可能使用 DigitalClock
。對於無法使用 DigitalClock
表示的所有其他文字或時鐘,PartText
是文字轉譯的容器。
視您要顯示圓形或一般文字而定,PartText
應包含 Text
或 TextCircular
元素。
使用字型和點陣圖字型
使用自訂字型,讓錶面以獨特風格脫穎而出。
在 TimeText
和 PartText
容器中使用自訂字型的做法有兩種。
在 Font
元素中指定自訂字型 family
。系統支援一系列常見格式,且必須放置在 res/font
例如,使用 Google Fonts 提供的 Pacifico 字型,並將資產放在 res/font/pacifico.ttf:
<PartText x="0" y="100" width="450" height="250">
<Text align="CENTER">
<Font family="pacifico" size="96">Hello!</Font>
</Text>
</PartText>
或者,您也可以在 res/drawable
中定義 BitmapFont
,以便提供點陣圖圖片:
<WatchFace width="450" height="450" clipShape="CIRCLE">
<BitmapFonts>
<BitmapFont name="myhandwriting">
<Character name="1" resource="digit1" width="50" height="100" />
<Character name="2" resource="digit2" width="50" height="100" />
<Character name="3" resource="digit3" width="50" height="100" />
<Character name="4" resource="digit4" width="50" height="100" />
<!-- ... -->
<!-- Treat "12" specially, instead of a 1 followed by a 2-->
<Word name="12" resource="digit12" width="80" height="100" />
</BitmapFont>
</BitmapFonts>
<!-- ... -->
請注意,字元序列可如何獲得特殊處理。舉例來說,如果要使用已彙整的 1 和 2 代表「12」,可以使用 Word
元素達成這項目標。
如要使用已定義的字型,請按照下列步驟操作:
<TimeText ... format="hh:mm">
<BitmapFont family="myhandwriting" size="48" color="#ff0000" />
</TimeText>
文字效果
錶面格式提供多種可套用的文字效果,例如 OutGlow
和 Shadow
。如要使用這些元素,請將這些元素設為 Font
元素的子元素:
<Font family="pacifico" size="96" color="#e2a0ff">
<OutGlow color="#e8ffb7" radius="30">Hello!</OutGlow>
</Font>
使用範本
您可能需要使用資料來源或運算式,而非靜態文字來建構文字。
Template
元素可讓您執行以下操作:
<PartText x="100" y="150" width="300" height="120" >
<Text align="CENTER">
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">
<Template>Day: %s<Parameter expression="[DAY_OF_WEEK_S]" /></Template>
</Font>
</Text>
</PartText>
使用資源
如果靜態文字是在資源中定義 (例如在 res/values/strings.xml
中),則可透過以下方式參照:
<PartText x="100" y="150" width="300" height="120" >
<Text align="CENTER">
<!-- greeting defined in res/values/strings.xml -->
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">greeting</Font>
</Text>
</PartText>
您也可以使用不同的資源限定詞,將錶面本地化。
把手間距
使用間距和文字時,可能會遇到一些問題:
<!-- greeting defined in res/values/strings.xml -->
<!-- Works correctly: -->
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">greeting</Font>
<!-- Does not render in the right place because of whitespace -->
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">
greeting
</Font>
這是因為 XML 中的空格很重要。為避免這種情況,請將 Font
內容包裝在 CDATA
元素中:
<!-- Works correctly -->
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">
<![CDATA[greeting]]>
</Font>
嘗試將文字置中對齊時,可以看到另一個例子:
<!-- Does not render as expected - leading spaces are a problem -->
<PartText x="100" y="150" width="250" height="120" >
<Text align="CENTER">
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">
Hello
</Font>
</Text>
</PartText>
<!-- Works correctly -->
<PartText x="100" y="150" width="250" height="120" >
<Text align="CENTER">
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">
<![CDATA[Hello]]>
</Font>
</Text>
</PartText>
多行文字
如要建立多行文字,請在 Text
上使用 maxLines
屬性:
<PartText x="75" y="100" width="300" height="350" >
<Text align="CENTER" maxLines="2">
<Font family="pacifico" size="60" weight="BOLD" color="#ffffff">
<![CDATA[Hello Wear OS world]]>
</Font>
</Text>
</PartText>
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Work with text\n\nFor digital clocks, you should aim to use `DigitalClock` where possible. For all\nother text or clocks that cannot be represented using `DigitalClock`,\n`PartText` is the container for text-based rendering.\n\nDepending on whether you want to show circular or regular text, the `PartText`\nshould contain *either* a `Text` or a `TextCircular` element.\n\n### Work with fonts and bitmap fonts\n\nUsing custom fonts allows your watch face to stand out with its own style.\n\nThere are two ways to use custom fonts, both within `TimeText` and `PartText`\ncontainers.\n\n1. Specify a custom font `family` in the `Font` element. [A range of common\n formats](/training/wearables/wff/group/part/text/font?version=1) are\n supported, which must be placed in `res/font`\n\n For example, using the Pacifico font from Google Fonts, and placing the\n asset as res/font/pacifico.ttf: \n\n \u003cPartText x=\"0\" y=\"100\" width=\"450\" height=\"250\"\u003e\n \u003cText align=\"CENTER\"\u003e\n \u003cFont family=\"pacifico\" size=\"96\"\u003eHello!\u003c/Font\u003e\n \u003c/Text\u003e\n \u003c/PartText\u003e\n\n2. Alternatively, define a `BitmapFont` supplying bitmap images in\n `res/drawable`:\n\n \u003cWatchFace width=\"450\" height=\"450\" clipShape=\"CIRCLE\"\u003e\n \u003cBitmapFonts\u003e\n \u003cBitmapFont name=\"myhandwriting\"\u003e\n \u003cCharacter name=\"1\" resource=\"digit1\" width=\"50\" height=\"100\" /\u003e\n \u003cCharacter name=\"2\" resource=\"digit2\" width=\"50\" height=\"100\" /\u003e\n \u003cCharacter name=\"3\" resource=\"digit3\" width=\"50\" height=\"100\" /\u003e\n \u003cCharacter name=\"4\" resource=\"digit4\" width=\"50\" height=\"100\" /\u003e\n \u003c!-- ... --\u003e\n \u003c!-- Treat \"12\" specially, instead of a 1 followed by a 2--\u003e\n \u003cWord name=\"12\" resource=\"digit12\" width=\"80\" height=\"100\" /\u003e\n \u003c/BitmapFont\u003e\n \u003c/BitmapFonts\u003e\n \u003c!-- ... --\u003e\n\nNote how sequences of characters can be given a special treatment. For example,\nif \"12\" was to be represented with a joined 1 and 2, this could be achieved\nusing a `Word` element.\n\nTo use the defined font: \n\n \u003cTimeText ... format=\"hh:mm\"\u003e\n \u003cBitmapFont family=\"myhandwriting\" size=\"48\" color=\"#ff0000\" /\u003e\n \u003c/TimeText\u003e\n\n### Text effects\n\nWatch Face Format provides several text effects which can be applied, such as\n[`OutGlow`](/training/wearables/wff/group/part/text/decoration/out-glow) and [`Shadow`](/training/wearables/wff/group/part/text/decoration/shadow). To use these, apply them as subelements of the\n`Font` element: \n\n \u003cFont family=\"pacifico\" size=\"96\" color=\"#e2a0ff\"\u003e\n \u003cOutGlow color=\"#e8ffb7\" radius=\"30\"\u003eHello!\u003c/OutGlow\u003e\n \u003c/Font\u003e\n\n### Work with templates\n\nInstead of static text, you may need to construct your text from data sources or\nexpressions.\n\nThe `Template` element lets you do this: \n\n \u003cPartText x=\"100\" y=\"150\" width=\"300\" height=\"120\" \u003e\n \u003cText align=\"CENTER\"\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003e\n \u003cTemplate\u003eDay: %s\u003cParameter expression=\"[DAY_OF_WEEK_S]\" /\u003e\u003c/Template\u003e\n \u003c/Font\u003e\n \u003c/Text\u003e\n \u003c/PartText\u003e\n\n### Work with resources\n\nIf your static text is instead defined in a resource, such as in\n`res/values/strings.xml`, then it can be referenced as follows: \n\n \u003cPartText x=\"100\" y=\"150\" width=\"300\" height=\"120\" \u003e\n \u003cText align=\"CENTER\"\u003e\n \u003c!-- greeting defined in res/values/strings.xml --\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003egreeting\u003c/Font\u003e\n \u003c/Text\u003e\n \u003c/PartText\u003e\n\nThis also lets you localize your watch face using different [resource\nqualifiers](/guide/topics/resources/providing-resources#AlternativeResources).\n\n### Handle spacing\n\nThere can be some challenges with working with spacing and text: \n\n \u003c!-- greeting defined in res/values/strings.xml --\u003e\n \u003c!-- Works correctly: --\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003egreeting\u003c/Font\u003e\n\n \u003c!-- Does not render in the right place because of whitespace --\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003e\n greeting\n \u003c/Font\u003e\n\nThis is because the spacing matters in XML. To avoid this situation, wrap your\n`Font` contents in a `CDATA` element: \n\n \u003c!-- Works correctly --\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003e\n \u003c![CDATA[greeting]]\u003e\n \u003c/Font\u003e\n\nAnother example can be seen when trying to center justify text: \n\n \u003c!-- Does not render as expected - leading spaces are a problem --\u003e\n \u003cPartText x=\"100\" y=\"150\" width=\"250\" height=\"120\" \u003e\n \u003cText align=\"CENTER\"\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003e\n Hello\n \u003c/Font\u003e\n \u003c/Text\u003e\n \u003c/PartText\u003e\n\n \u003c!-- Works correctly --\u003e\n \u003cPartText x=\"100\" y=\"150\" width=\"250\" height=\"120\" \u003e\n \u003cText align=\"CENTER\"\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003e\n \u003c![CDATA[Hello]]\u003e\n \u003c/Font\u003e\n \u003c/Text\u003e\n \u003c/PartText\u003e\n\n### Multiline text\n\nTo create multiline text, use the `maxLines` attribute on `Text`: \n\n \u003cPartText x=\"75\" y=\"100\" width=\"300\" height=\"350\" \u003e\n \u003cText align=\"CENTER\" maxLines=\"2\"\u003e\n \u003cFont family=\"pacifico\" size=\"60\" weight=\"BOLD\" color=\"#ffffff\"\u003e\n \u003c![CDATA[Hello Wear OS world]]\u003e\n \u003c/Font\u003e\n \u003c/Text\u003e\n \u003c/PartText\u003e"]]