將元素排列成群組
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用群組,將錶面設計分割成邏輯結構。
這麼做有助於整理元件。您可以為每個群組指定 name
,以便指出其用途。
群組之所以非常實用,另一個原因是您可以將群組內的所有內容視為單一實體,藉此調整外觀,甚至動態調整以因應變更的資料來源。
以下列舉一些實用範例。以下 Group
包含各種 PartText, PartImage
和 PartDraw
元素,這些元素組成錶面的單一邏輯部分:
<Group name="decorations" ...>
<PartText ...>
<PartImage ...>
<PartDraw ...>
</Group>
變更微光模式行為
定義 Group
後,您可以調整整個 Group
的顯示設定,例如隱藏 Group
,以便使用微光模式:
<Group ...>
<Variant mode="AMBIENT" target="alpha" value="0" />
<PartText ...>
<PartImage ...>
<PartDraw ...>
</Group>
這樣一來,您就不必在每個子元素中個別新增 Variant
元素。
與使用 Variant
調整環境行為類似,您也可以使用一或多個 Transform
元素調整 Group
的許多屬性。
在本例中,Group
會根據秒數旋轉。將 pivotX
和 pivotY
指定為 0.5
後,旋轉動作會以 Group
的中心為圓心,無論 PartText
或 PartImage
在該 Group
中的位置為何皆是如此:
<Group x="0" y="0" width="450" height="450" pivotX="0.5" pivotY="0.5">
<!-- One full rotation per minute -->
<Transform target="angle" value="[SECOND] * 6" />
<PartText ...>
<PartImage ...>
<PartDraw ...>
</Group>
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Arrange elements into groups\n\nGroups allow you to separate your watch face design into a logical structure.\n\nThis can be useful to help you organize components. You can give each\ngroup a `name` to indicate what it is for.\n\nA further reason that groups are very useful is that you can then treat\neverything within that group as a single entity for the purpose of adjusting its\nappearance, even dynamically adjusting to changing data sources.\n\nHere are some examples of how this can be useful. The following `Group` contains\nvarious `PartText, PartImage` and `PartDraw` elements that make up a single\nlogical part of the watch face: \n\n \u003cGroup name=\"decorations\" ...\u003e\n \u003cPartText ...\u003e\n \u003cPartImage ...\u003e\n \u003cPartDraw ...\u003e\n \u003c/Group\u003e\n\nChange ambient behavior\n-----------------------\n\nHaving defined the `Group`, the visibility of the whole `Group` can be adjusted\nfor Ambient mode, for example, to hide the `Group`: \n\n \u003cGroup ...\u003e\n \u003cVariant mode=\"AMBIENT\" target=\"alpha\" value=\"0\" /\u003e\n \u003cPartText ...\u003e\n \u003cPartImage ...\u003e\n \u003cPartDraw ...\u003e\n \u003c/Group\u003e\n\nThis avoids the need to add a `Variant` element individually to each child\nelement.\n\n### Transform a group\n\nSimilar to adjusting the ambient behavior, using `Variant`, many of the\nproperties of `Group` can be adjusted using one or more `Transform` elements.\n\nIn this example, the `Group` is rotated based on the second. By specifying\n`pivotX` and `pivotY` as `0.5`, the rotation occurs around the center of the\n`Group`, irrespective of where each element, such as `PartText` or `PartImage`\nsits within that `Group`: \n\n \u003cGroup x=\"0\" y=\"0\" width=\"450\" height=\"450\" pivotX=\"0.5\" pivotY=\"0.5\"\u003e\n \u003c!-- One full rotation per minute --\u003e\n \u003cTransform target=\"angle\" value=\"[SECOND] * 6\" /\u003e\n \u003cPartText ...\u003e\n \u003cPartImage ...\u003e\n \u003cPartDraw ...\u003e\n \u003c/Group\u003e"]]