處理文字

對於數位時鐘,您應盡可能使用 DigitalClock。對於無法使用 DigitalClock 表示的所有其他文字或時鐘,PartText 是文字轉譯的容器。

視您要顯示圓形或一般文字而定,PartText 應包含 TextTextCircular 元素。

使用字型和點陣圖字型

使用自訂字型,讓錶面以獨特風格脫穎而出。

TimeTextPartText 容器中使用自訂字型的做法有兩種。

  1. 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>
    
  2. 或者,您也可以在 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>

文字效果

錶面格式提供多種可套用的文字效果,例如 OutGlowShadow。如要使用這些元素,請將這些元素設為 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>