ZStack

把多个 View 叠放在同一区域,适合背景、刻度与中心读数。子 View 按顺序绘制,alignment 设置它们在容器内的共同对齐位置。

示例

function main() {
  return (
    <ZStack alignment="center">
      <Circle width={100} height={100} background={$env.accent} />
      <Text color="black">78</Text>
    </ZStack>
  );
}

用法说明

  • 子 View 按顺序从后向前叠放,zIndex 可改变绘制顺序。alignment 把每个子 View 对到容器的同一个位置;容器与子 View 大小相同时,不容易看出对齐差别。
  • 数字尺寸使用屏幕点;auto 根据内容测量,fill 使用父布局分配的空间。先给容器明确尺寸,再调整子 View。fill 不会创建额外空间,长文本也不会自动撑开固定尺寸的父级。
  • 布局先决定占位,再应用 x/y 和 transform。把一个子 View 右移 20 点,不会把后面的兄弟推开;要改变间隔用 spacing 或 Spacer。旋转、缩放会作用于已经绘制好的整个 View。

属性

children?ViewContent

子视图内容。

alignment?"topLeading" | "top" | "topTrailing" | "leading" | "center" | "trailing" | "bottomLeading" | "bottom" | "bottomTrailing"

叠放的子 View 在容器中的对齐位置,支持四角、四边及中心共九种位置。

示例"center"
通用属性
width?number | "auto" | "fill"

宽度:点数、自动测量或填满父区域。

示例100
height?number | "auto" | "fill"

高度:点数、自动测量或填满父区域。

示例80
padding?number

内容内边距,单位为点。

示例0
x?number

视觉偏移,单位为点:x 向右、y 向下,不改变布局占位。

示例0
y?number

视觉偏移,单位为点:x 向右、y 向下,不改变布局占位。

示例0
transform?ViewTransform

对整个 View 做视觉变换,不改变布局。平移用点,旋转和倾斜用角度,缩放 1 保持原大小,锚点用 0–1。

示例{rotation: 0}
background?Paint

背景填充,可使用颜色或渐变;clear 表示透明。

示例"clear"
opacity?number

整个 View 的不透明度:0 完全透明,1 完全不透明。

示例1
cornerRadius?number

圆角半径,单位为点。

示例0
clipped?boolean

是否裁剪超出 View 边界的绘制内容。

示例false
clip?"rectangle" | "ellipse"

裁剪形状:rectangle 矩形或 ellipse 椭圆。Material 还可用 false 关闭裁剪。

示例"rectangle"
mask?View | null

透明度遮罩 View,居中显示且不影响布局;null 表示不使用遮罩。

示例<Circle width="fill" height="fill" background="white"/>
zIndex?number

同级 View 的绘制顺序,数值越大越靠前。

示例0
blur?number

模糊半径,单位为点;0 表示不模糊。

示例4
innerShadow?DrawingEffect

特效的颜色、不透明度、模糊半径和 x/y 偏移;距离单位为点。

示例{color: HDR("black"), opacity: 0.5, radius: 4, x: 0, y: 2}

特效的颜色、不透明度、模糊半径和 x/y 偏移;距离单位为点。

示例{color: HDR("cyan"), radius: 8, opacity: 0.8}
shadowColor?Color

外部阴影的颜色。

示例HDR("black")
shadowOpacity?number

外部阴影的不透明度,范围 0–1;0 隐藏阴影。

示例0
shadowRadius?number

外部阴影的模糊半径,单位为点。

示例4
shadowX?number

外部阴影偏移,单位为点:x 向右,y 向下。

示例0
shadowY?number

外部阴影偏移,单位为点:x 向右,y 向下。

示例2
animations?Animation[]

支持的属性变化时执行动画。duration/delay 单位为秒;from/to 指定端点;repeat 和 autoreverse 控制重复,wrap 指定周期。

示例[{property: "opacity", duration: 0.25, curve: "easeInOut"}]
key?string | number

兄弟节点中的稳定标识。

组合示例

function main() {
  return (
    <ZStack width={180} height={100} alignment="bottomTrailing">
      <Rectangle
        width="fill"
        height="fill"
        background="#30B3A233"
        cornerRadius={12}
      />
      <Text fontSize={40} x={-12} y={-8}>
        78
      </Text>
    </ZStack>
  );
}

动画

动画

widthheightpaddingxyopacitybackgroundcornerRadiusblurshadowColorshadowOpacityshadowRadiusshadowXshadowYtransform.translateXtransform.translateYtransform.scaleXtransform.scaleYtransform.rotationtransform.skewXtransform.skewYtransform.anchorXtransform.anchorY

省略时的行为

下表只列经实现核对的省略行为;属性旁的示例是可复制写法,不是默认值。需要固定效果时显式传值,尺寸、主题颜色等随上下文变化的属性不要当作固定常量。

属性省略时的行为
alignment"center"