使用文本
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对于数字时钟,您应尽可能使用 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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"]]