鸿蒙HarmonyOS实战-ArkUI组件(Stack)_harmonyos stack,2024年最新面试有技巧吗

52 阅读3分钟

在HarmonyOS中,开发者可以通过使用ArkUI提供的Stack布局来快速构建具有层叠效果的界面。Stack布局支持多种控件的布局方式,比如文本、图像、按钮等等。开发者可以根据自己的需要选择不同的控件进行布局。通过合理的设计和排布,可以让应用程序在不同的设备上呈现出更加统一、美观的效果。

具有如图:

Column(){ Stack({ }) { Column(){}.width('90%').height('100%').backgroundColor('#ff58b87c') Text('text').width('60%').height('60%').backgroundColor('#ffc3f6aa') Button('button').width('30%').height('30%').backgroundColor('#ff8ff3eb').fontColor('#000') }.width('100%').height(150).margin({ top: 50 }) }

在这里插入图片描述

3.对齐方式

ArkUI中Stack的alignContent属性用于定义Stack内的所有子元素在交叉轴上的排列方式。该属性仅在Stack容器内含有多行子元素时才会生效。
在这里插入图片描述

4.Z序控制

在HarmonyOS中,层叠布局(Stack)的zIndex属性用于指定子布局的叠放顺序,决定了子布局的显示先后顺序。zIndex属性值较大的子布局会在zIndex属性值较小的子布局的上方显示。例如,zIndex属性值为2的子布局会在zIndex属性值为1的子布局的上方显示。

如果两个子布局的zIndex属性值相同,则它们的显示顺序将按照它们在布局中的位置确定。越靠后的子布局会在越靠前的子布局的上方显示。

值得注意的是,只有在使用层叠布局(Stack)时,zIndex属性才会起作用。如果使用线性布局或网格布局等其他类型的布局,则zIndex属性不会起作用。

Stack({ alignContent: Alignment.BottomStart }) { Column() { Text('Stack子元素1').fontSize(20) }.width(100).height(100).backgroundColor(0xffd306).zIndex(2)

Column() { Text('Stack子元素2').fontSize(20) }.width(150).height(150).backgroundColor(Color.Pink).zIndex(1)

Column() { Text('Stack子元素3').fontSize(20) }.width(200).height(200).backgroundColor(Color.Grey) }.margin({ top: 100 }).width(350).height(350).backgroundColor(0xe0e0e0)

在这里插入图片描述

5.宫格案例

@Entry @Component struct StackSample { private arr: string[] = ['APP1', 'APP2', 'APP3', 'APP4', 'APP5', 'APP6', 'APP7', 'APP8'];

build() { Stack({ alignContent: Alignment.Bottom }) { Flex({ wrap: FlexWrap.Wrap }) { ForEach(this.arr, (item) => { Text(item) .width(100) .height(100) .fontSize(16) .margin(10) .textAlign(TextAlign.Center) .borderRadius(10) .backgroundColor(0xFFFFFF) }, item => item) }.width('100%').height('100%') Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) { Text('联系人').fontSize(16) Text('设置').fontSize(16) Text('短信').fontSize(16) } .width('50%') .height(50) .backgroundColor('#16302e2e') .margin({ bottom: 15 }) .borderRadius(15) }.width('100%').height('100%').backgroundColor('#CFD0CF') } }

在这里插入图片描述

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!