定义用户配置

UserConfigurations 可让您创建供用户选择的选项。您可以根据所选值调整表盘元素的外观。

用户配置选项可以是:

  • BooleanConfiguration:通常用于用户可以选择显示或不显示元素,或者在两种样式之间进行选择的情况
  • ListConfiguration:为用户提供一系列选项。例如,如果表盘有四种不同的背景图片可供选择
  • ColorConfiguration:定义颜色主题,用户可以从中选择自己喜欢的主题。

布尔值选项

布尔值选项可能是最简单的用户配置。它们可以定义如下:

<!-- Under WatchFace element -->
<UserConfigurations>
    <!-- show_date and show_date_label defined in res/values/strings.xml -->
    <BooleanConfiguration id="show_date"
        displayName="show_date_label"
        screenReaderText="show_date_label"
        defaultValue="TRUE"
        />
</UserConfigurations>

然后,可以通过两种方式使用布尔值选项:

  1. 在表盘 Scene 中使用 BooleanConfiguration 结构:

    <!-- Within the main Scene of the watch face -->
    <BooleanConfiguration id="show_date">
        <BooleanOption id="TRUE">
            <!-- ...Content when date required -->
        </BooleanOption>
        <BooleanOption id="FALSE">
            <!-- ...Content when date not required -->
        </BooleanOption>
    </BooleanConfiguration>

    请注意,配置选项不能嵌套使用。

  2. 或者,您可以在表达式中使用此配置选项:

        <Expressions>
            <Expression name="my_expression">
                <!-- Use show_date as part of a more complex evaluation -->
                <![CDATA[[CONFIGURATION.show_date] == "TRUE" && [HOUR_0_23] < 15]]
            </Expression>
        </Expressions>
        <Compare expression="my_expression">
            <!-- Content goes here -->
        </Compare>
    </Condition>

列表选项

列表选项的工作方式与布尔值选项非常相似。例如,如需提供供用户选择的背景图片列表,请执行以下操作:

<!-- Under WatchFace element -->
<UserConfigurations>
    <ListConfiguration id="background_image" displayName="background_image_label"
        icon="background_option_icon"
        screenReaderText="background_image_label" defaultValue="0">
        <ListOption id="0" displayName="background0_image_label"
            screenReaderText="background0_image_label" icon="background0_icon" />
        <ListOption id="1" displayName="background1_image_label"
            screenReaderText="background1_image_label" icon="background1_icon" />
        ...
    </ListConfiguration>
</UserConfigurations>

与布尔值选项类似,您可以通过以下两种方式使用此选项:

  1. Scene 中使用 ListConfiguration 元素:

    <!-- Within the main Scene of the watch face -->
    <ListConfiguration id="background_image">
        <ListOption id="0">
            <!-- ...Content for option 0 -->
        </ListOption>
        <ListOption id="1">
            <!-- ...Content for option 1 -->
        </ListOption>
    </ListConfiguration>

  2. 或者,该配置选项可用于更复杂的表达式:

        <Expressions>
            <Expression name="background_zero_and_something_else">
                <!-- Use as part of a more complex evaluation -->
                <![CDATA[[CONFIGURATION.background_image] == "0" && [HOUR_0_23] < 15]]
            </Expression>
        </Expressions>
        <Compare expression="background_zero_and_something_else">
            <!-- Content goes here -->
        </Compare>
    </Condition>

颜色主题

借助表盘格式,您可以通过 ColorConfiguration 定义颜色主题。用户可以在表盘编辑器中选择自己喜欢的主题,然后该主题中的颜色便会显示在整个表盘定义中。

例如,如需定义一个包含两个条目且主题中有三种颜色的主题,请按如下方式定义 ColorConfiguration

<!-- Under WatchFace element -->
<UserConfigurations>
    <ColorConfiguration id="myThemeColor" displayName="theme_label" defaultValue="0">
        <ColorOption id="0" displayName="relaxed_label" colors="#3083dc #f8ffe5 #7dde92" />
        <ColorOption id="1" displayName="urban_label" colors="#f4b393 #fc60a8 #7a28cb" />
    </ColorConfiguration>
</UserConfigurations>

然后,这些值可用作数据源,而不是十六进制颜色值。请注意,如何指定索引值来选择主题的第一个、第二个或第三个元素:

<AnalogClock x="0" y="0" width="450" height="450">
    <HourHand resource="hour" x="220" y="55" width="20" height="190"
        pivotX="0.5" pivotY="0.9210"
        tintColor="[CONFIGURATION.myThemeColor.0]"/>
    <MinuteHand resource="minute" x="222" y="30" width="16" height="220"
        pivotX="0.5" pivotY="0.9"
        tintColor="[CONFIGURATION.myThemeColor.1]"/>
    <SecondHand resource="second" x="226" y="20" width="8" height="245"
        pivotX="0.5" pivotY="0.8571"
        tintColor="[CONFIGURATION.myThemeColor.2]"/>
</AnalogClock>

在每个 ColorOption 仅定义了一种颜色的特定情况下,也可以将其引用为 CONFIGURATION.myThemeColor,而无需索引。然后,用户可以在表盘编辑器中选择所需的主题条目。

口味

注意:表盘格式版本 2 及更高版本支持风格。

UserConfigurations 为用户提供了很大的灵活性,但随着您定义的配置元素数量的增加,组合数量可能会急剧增加。

借助 Flavors,您可以为认为值得突出显示的 UserConfigurations 定义预设

然后,用户可以在配套应用中从这些预设变种中进行选择,也可以继续单独选择每个配置值。

例如,假设您定义了以下三项设置:

  1. 一种颜色主题配置,允许用户选择要应用的颜色主题。您已定义两个主题,一个彩色主题和一个单色主题。
  2. 背景列表。您已定义两个可供用户选择的选项。
  3. 用于选择是否在表盘上显示用户的心率。

此外,表盘上还会显示 ComplicationSlot

您决定向用户突出显示两个 Flavors。 这些设置还有许多可能的组合,但您认为以下组合效果最好:

  1. 运动风格:包含以下内容:
    1. 鲜艳的色彩主题,让您充满活力,积极行动起来(ID:0)
    2. 第一个背景图片(ID:0)
    3. 表盘上显示的心率,供参考
    4. 显示步数的复杂功能显示槽
  2. 精致风味:包含以下内容:
    1. 单色主题,可搭配任何服装(ID:1)
    2. 第二个背景图片(ID:1)
    3. 表盘上不显示心率
    4. 未启用功能块插槽

需要在 watch_face_info.xml 中启用 build flavor,因此 watch_face_info.xml 文件中的 FlavorsSupported 元素应设置为 value="true"

每个 build 风味都在 UserConfigurations 中定义,如下所示:

<!-- Under UserConfigurations -->
<Flavors defaultValue="sporty_flavor">
    <Flavor id="sporty_flavor"
        displayName="flavor_sporty_label" screenReaderText="flavor_sporty_label">
        <Configuration id="theme_color" optionId="0"/>
        <Configuration id="background_image" optionId="0"/>
        <Configuration id="show_hr" optionId="TRUE"/>
        <ComplicationSlot slotId="0">
            <DefaultProviderPolicy
                defaultSystemProvider="STEP_COUNT"
                defaultSystemProviderType="SHORT_TEXT"/>
        </ComplicationSlot>
    </Flavor>
    <Flavor id="sophisticated_flavor"
        displayName="flavor_sophisticated_label" screenReaderText="flavor_sophisticated_label">
        <Configuration id="theme_color" optionId="1"/>
        <Configuration id="background_image" optionId="1"/>
        <Configuration id="show_hr" optionId="FALSE"/>
        <ComplicationSlot slotId="0">
            <!--
              Type here is set to empty to demonstrate how to hide a complication
              slot in Flavors.
            -->
            <DefaultProviderPolicy
                defaultSystemProvider="SUNRISE_SUNSET"
                defaultSystemProviderType="EMPTY"/>
        </ComplicationSlot>
    </Flavor>
</Flavors>