iis 发布asp网站,软件开发模型的优缺点及适用范围,wordpress 锚点插件,h5建站工具系列文章目录 
【鸿蒙】HarmonyOS NEXT开发快速入门教程之ArkTS语法装饰器#xff08;上#xff09; 【鸿蒙】HarmonyOS NEXT开发快速入门教程之ArkTS语法装饰器#xff08;下#xff09; 【鸿蒙】HarmonyOS NEXT应用开发快速入门教程之布局篇#xff08;上#xff09; 【…系列文章目录 
【鸿蒙】HarmonyOS NEXT开发快速入门教程之ArkTS语法装饰器上 【鸿蒙】HarmonyOS NEXT开发快速入门教程之ArkTS语法装饰器下 【鸿蒙】HarmonyOS NEXT应用开发快速入门教程之布局篇上 【鸿蒙】HarmonyOS NEXT应用开发快速入门教程之布局篇下 文章目录 系列文章目录前言一、常见布局详细使用4相对布局RelativeContainer1、基本概念2、语法提炼3、 对齐方式水平方向垂直方向 4、 使用示例示例1示例2示例3示例4示例5  5相对布局2offset示例示例1示例2 6绝对布局position示例1示例2示例3示例4示例5  二、布局中的渲染控制1. if/else2. ForEach示例1示例2  三、项目开发过程中布局像素单位的选择1、像素单位介绍pxvpfplpx 2、像素单位之间的转换3、实际开发单位的选择lpx基准宽designWidth设置  四、结束 前言 
HarmonyOS NEXT鸿蒙应用开发快速入门教程之布局篇(下)基于HarmonyOS NEXT Beta1版本api 12讲解。 本文将从前端开发者角度来学习鸿蒙的布局语法通过类比鸿蒙布局和web css布局相似之处帮助大家快速掌握鸿蒙布局开发。 一、常见布局详细使用 
4相对布局RelativeContainer 此相对布局非css里面的相对布局而是安卓里面的相对布局对于web开发人员来说比较新的一种布局方式而对于安卓开发来说就非常熟悉css相对布局在鸿蒙里面也有类似用法后续会讲到。 RelativeContainer为采用相对布局的容器支持容器内部的子元素设置相对位置关系适用于界面复杂场景的情况对多个子组件进行对齐和排列。子元素支持指定兄弟元素作为锚点也支持指定父容器作为锚点基于锚点做相对位置布局。 
通俗介绍说就是子元素放置位置既可以设置相对父容器也可以设置相对兄弟组件相对父元素就可以看做在一个密闭长方形空间你想把东西放置它的上下左右某个对齐方向上相对兄弟组件可以看成两个盒子让盒子上面对齐排列或者盒子上和另一个盒子下对齐左对齐或者左与右对齐确定子元素位置。所以这边相对的是其他组件而css的相对的是自己因此必须设置相对的元素这个元素称为锚点。 
1、基本概念 
锚点通过锚点设置当前元素基于哪个元素确定位置。 对齐方式通过对齐方式设置当前元素是基于锚点的上中下对齐还是基于锚点的左中右对齐。 
2、语法提炼 
RelativeContainer() {子组件().alignRules({方向(left或right或top或bottom):{anchor:锚点ID,align:对齐枚举值},方向(left或right或top或bottom):{anchor:锚点ID,align:对齐枚举值},....})
}一般设置2个方向就能确定位置了比如lefttop,如果子组件未设置高度或宽度的也可以通过设置3个以上方向让子组件拉伸填满父组件宽度或高度。 为了明确定义锚点必须为RelativeContainer及其子元素设置IDRelativeContainer不设置默认id为“ __ container __ ”其余子元素的ID通过id属性设置。不设置id的组件能显示但是不能被其他子组件作为锚点 3、 对齐方式 
align值对应枚举值 
水平方向 
HorizontalAlign.Start 左 HorizontalAlign.Center 中 align:HorizontalAlign.End 右 
垂直方向 
VerticalAlign.Top 上 VerticalAlign.Center 中 VerticalAlign.Bottom 下 
4、 使用示例 
示例1 
Entry
Component
struct Index {build() {RelativeContainer() {Button(按钮).alignRules({left: { anchor: __container__, align: HorizontalAlign.Start },bottom: { anchor: __container__, align: VerticalAlign.Bottom } })}.width(100%).height(100%)}
} 说明按钮水平方向基于父容器左边对齐垂直方向基于父容器底部对齐 运行效果  
示例2 
Entry
Component
struct Index {build() {RelativeContainer() {Button(按钮).alignRules({right: { anchor: __container__, align: HorizontalAlign.Center },top: { anchor: __container__, align: VerticalAlign.Center } }).width(100).height(150)}.width(100%).height(100%)}
} 说明按钮的右边与父容器水平方向中间对齐顶部与父容器纵向中间对齐 运行效果  
示例3 
Entry
Component
struct Index {build() {RelativeContainer() {Column() {Text(矩形)}.width(100).height(50).backgroundColor(#ff22f2).margin({top: 80,left: 100}).id(rect)Button(按钮).alignRules({left: { anchor: rect, align: HorizontalAlign.End },top: { anchor: rect, align: VerticalAlign.Bottom }}).width(100).height(150)}.width(100%).height(100%)}
} 说明按钮左边与矩形右边对齐按钮顶部与按钮底部对齐 运行效果  
示例4 
Entry
Component
struct Index {build() {RelativeContainer() {Column() {Text(矩形)}.width(100).height(50).backgroundColor(#ff22f2).margin({top: 80,left: 100}).id(rect)Button(按钮).alignRules({left: { anchor: rect, align: HorizontalAlign.End },top: { anchor: rect, align: VerticalAlign.Bottom }}).id(button).width(100).height(150)Button(按钮2).alignRules({right: { anchor: button, align: HorizontalAlign.Start },top: { anchor: button, align: VerticalAlign.Bottom }}).width(100).height(150)}.width(100%).height(100%)}
} 说明 按钮1左边与矩形右边对齐按钮1顶部与按钮底部对齐 按钮2右边与按钮1左边对齐按钮2顶部与按钮1底部对齐 运行效果  
示例5 
Entry
Component
struct Index {build() {RelativeContainer() {Button(按钮).alignRules({right: { anchor: __container__, align: HorizontalAlign.End },top: { anchor: __container__, align: VerticalAlign.Top },bottom: { anchor: __container__, align: VerticalAlign.Bottom },})}}
}说明通过设置按钮顶部贴父容器顶部底部贴父容器底部按钮高度自动填满父元素运行效果  
5相对布局2offset 通用属性offset的作用跟css相对布局是一样的相对于自身的位置进行偏移不影响父容器布局。 基本语法 
组件.offset({x:10,y:10})组件.offset({x:-10,y:-10})
组件.offset({x:10px,y:10vp})组件.offset({x:20%,y:10})说明 1、x表示水平方向偏移量y表示纵向方法偏移量值单位默认vp也可以字符串自带单位。 2、x为负值表示向左偏移正值向右偏移同理y为负值表示向上偏移正值向下偏移。 3、值也支持%字符串百分比相对的是父容器的宽或高例如x:50%,则组件向右偏移父容器宽度的50% 
示例 
按钮初始位置在屏幕左上角 
Entry
Component
struct Index {build() {Column(){Button(按钮)}.width(100%).height(500).backgroundColor(Color.Green).alignItems(HorizontalAlign.Start)}
}示例1 
Entry
Component
struct Index {build() {Column(){Button(按钮).offset({x:100,y:100})}.width(100%).height(500).backgroundColor(Color.Green).alignItems(HorizontalAlign.Start)}
} 
运行效果 示例2 
Entry
Component
struct Index {build() {Column(){Button(按钮).offset({x:50%,y:0})}.width(100%).height(500).backgroundColor(Color.Green).alignItems(HorizontalAlign.Start)}
} 
运行效果 6绝对布局position 通用属性position的作用跟css绝对布局是一样的子组件相对父容器确定位置当父容器为Row/Column/Flex时设置position的子组件不占位不影响其他兄弟子组件正常布局。 基本语法 
组件.position({x:10,y:10})组件.position({x:-10,y:-10})组件.position({x:50%,y:10px})组件.position({left:10,top:10})
组件.position({right:10,bottom:10}) 
说明 1、入参类型既支持Position(x\y)类型也支持Edgestop\bottom\left\right类型 2、和offset一样值支持字符串单位、百分比、数字类型默认单位vp支持正负值。 3、Edgestop\bottom\left\right类型用法跟css基本一致top相对父容器顶部距离bottom相对父容器底部距离left相对父容器左边距离right相对父容器右边距离通过边距来确定组件相对于父组件的位置。只需设置2个方向就能确定位置比如topleft或者bottomright同时设置top和bottom仅top生效同时设置left和right仅left生效 4、Position(x\y)类型的x\y实际等同于Edges left\top,xleft,ytop 5、与css不同的是鸿蒙的绝对布局相对父容器起始位置需要扣去padding部分相当于ie盒子模型内容部分。 
示例1 
Entry
Component
struct Index {build() {Column(){Button(按钮).position({x:0,y:500px})}.width(100%).height(500).backgroundColor(Color.Green).alignItems(HorizontalAlign.Start)}
} 
运行效果 示例2 
Entry
Component
struct Index {build() {Column(){Button(按钮).position({left:0,bottom:-25}).width(100).height(50)}.width(100%).height(500).backgroundColor(Color.Green).alignItems(HorizontalAlign.Start)}
} 
运行效果  说明按钮底部和父容器底部相距-25vp也即按钮向下平移了25vp等于按钮自身高度一半 示例3 
Entry
Component
struct Index {build() {Column(){Button(按钮).position({right:0,bottom:0}).width(100).height(50)}.width(100%).height(500).backgroundColor(Color.Green).alignItems(HorizontalAlign.Start)}
} 
运行效果  说明按钮和父容器右边和底部贴合距离为0所以按钮位置在父容器右下角 示例4 子组件位于父组件水平垂直居中通用方法 Entry
Component
struct Index {build() {Column() {Button(按钮).position({left: 50%,top: 50%}).translate({x: -50%,y: -50%}).width(100).height(50)}.width(100%).height(500).backgroundColor(Color.Green).alignItems(HorizontalAlign.Start)}
} 
运行效果  说明positiontranslate可以实现任意子组件水平垂直居中position分别设置了50%此时子组件距离父组件上边和左边都为父组件宽高一半距离此时子组件的左上角点刚好在父组件正中心点。想要让子组件居中只需让子组件的中心点和父组件中心重叠把子组件向左和向上平移自身一半宽高即可可以用通用属性translate进行平移 .translate({x: -50%,y: -50%}此处百分比相对组件本身宽高负值表示往上或往左平移 
示例5 父容器带padding情况 Entry
Component
struct Index {build() {Column(){Button(按钮).position({top:0,left:0}).width(100).height(50)}.width(100%).height(500).backgroundColor(Color.Green).padding(50)}
}运行效果  说明从运行效果可以看出相对父容器起始位置需要扣除父容器的padding部分 二、布局中的渲染控制 在vue中可以使用v-ifv-else条件语句控制子组件是否渲染也可以通过v-for循环渲染多个子组件。鸿蒙也有类似用法分别为if/else和ForEach 1. if/else 
布局渲染条件控制语句 
示例 
Entry
Component
struct Index {State type:number0build() {Column() {//显示文本if(this.type0){Text(文本)}//显示按钮else if(this.type1){Button(按钮)}//显示文本输入框else{TextInput({placeholder:文本输入框})}Button(改变类型).onClick((){this.typethis.type%3}).margin({top:100})}.width(100%).height(100%).justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)}
} 
运行效果 说明通过改变状态变量type动态控制显示的子组件 需要注意if else内部只能写入子组件不能写入arkTs语法比如打印信息console.log()就会报错。 2. ForEach 
布局循环渲染控制语句 语法 
ForEach(list:ArrayESObject,fn:(item:ESObject,index:number)void,fn:(item:ESObject,index:number)string)
其中item为数组当前元素值index为循环的索引ForEach有三个入参第一个入参是数据源数组第二个入参是个函数内部添加要循环渲染的子组件第三个参数是函数返回唯一标识key第三个入参类似vue v-for 的key作用为了避免重复渲染导致的额外开销提高性能所以返回的key必须唯一值不然会异常 示例1 
Entry
Component
struct Index {State list: number[]  [1, 2, 3, 4, 5, 6]build() {Column() {ForEach(this.list, (item: number, index: number)  {//需要循环渲染的子组件Row() {Text(内容为${item},第${index}行)}.padding(15).justifyContent(FlexAlign.Start).width(100%)//}, (item: number, index: number)  index  )}.width(100%).height(100%)}
}运行结果  
示例2 
List和ListItem系统组件(列表)使用 
Entry
Component
struct Index {State list: number[]  [1, 2, 3, 4, 5, 6]build() {List() {ForEach(this.list, (item: number, index: number)  {ListItem() {Text(${index})}.height(60).width(100%).border({width:1,color:#f2f2f2})}, (item: number, index: number)  index  )}}
} 
运行结果 ps如果列表数据非常多加载卡顿也可以使用懒加载LazyForEach请看官网文档LazyForEach数据懒加载这里不再介绍。 三、项目开发过程中布局像素单位的选择 
1、像素单位介绍 鸿蒙布局像素单位支持px、vp、lpx、fp4种单位其中fp主要用来设置字体大小数值不写单位默认是vp px 
屏幕物理像素单位。 
vp 
虚拟像素单位类似安卓里面的dpVP是根据屏幕的PPI每英寸像素点数进行换算的。具体换算公式为vp  (px * 160) / PPI在480PPI手机上1vp3px简而言之vp是屏幕密度相关像素根据屏幕像素密度转换为屏幕物理像素。 
fp 
字体像素与vp类似适用屏幕密度变化区别在于fp会随系统字体大小设置变化。即默认情况下 1 fp  1vp。如果用户在设置中选择了更大的字体字体的实际显示大小就会在 vp 的基础上乘以 scale 系数即 1 fp  1 vp * scale 
lpx 
视窗逻辑像素单位lpx单位为实际屏幕宽度与逻辑宽度通过designWidth配置的比值designWidth默认值为720。当designWidth为720时在实际宽度为1440物理像素的屏幕上1lpx为2px大小。 lpx类似css里面的rem或者小程序里面的rpx单位以屏幕宽度为基准一种比值逻辑单位 2、像素单位之间的转换 鸿蒙sdk已自带单位换算方法直接调用即可如下 示例 //vp转px单位vp2px(20)//px转lpxpx2lpx(20)//px转vppx2vp(20)//vp转lpx没有直接转的函数所以需要先把vp转px在把px转lpxpx2lpx(vp2px(20))3、实际开发单位的选择 
实际开发中我们需要适配所有设备 选择某个单位使得布局在所有设备显示比例一样。从上面介绍看vp和lpx都能满足我们的需求尽管vp是默认单位也是官方推荐使用的单位但在实际开发中我们拿到设计稿尺寸跟vp换算复杂无法直接计算出来在1440屏幕宽度的设备上1vp大约3px假如从设计稿读取尺寸为285px,换算为vp285/3每个地方都要这样心算就比较费劲了。当然你也可以选择用px2vp285,但是每个地方都这样写代码显得囊肿。所以推荐使用lpx作为开发单位。如果你是web开发出身你完全可以把lpx当做rem单位理解鸿蒙的lpx支持我们自定义基准宽designWidth所以我们能轻松通过自定义designWidth值使得设计稿1px1lpx。这样就不需要复杂换算设计稿读取值多少就写多少。 
lpx基准宽designWidth设置 
路径 entry/src/main/resources/base/profile/main_pages.json添加 
window: {designWidth: 750}拿到的设计稿宽多少designWidth值就设置多少比如设计稿是1080x1920designWidth设置为1080。 这样设置完毕1px1lpx 
验证 
Entry
Component
struct Index {build() {Column() {Text(750lpx).width(750lpx).height(100lpx).backgroundColor(Color.Red)Text(730lpx).width(730lpx).height(100lpx).backgroundColor(Color.Yellow)Text(375lpx).width(375lpx).height(100lpx).backgroundColor(Color.Blue)}.alignItems(HorizontalAlign.Start)}
}运行效果  从运行效果可以看出当宽设置为750lpx刚好铺满屏幕730lpx有一点间隙375lpx占屏幕一半符合预期效果验证成功。 
四、结束 
结束前还是顺便提下鸿蒙布局里有个很常用的通用属性跟css命名长得最不一样可能很多人一开始都找不到。 
css中有个overflow: hidden 表示子元素超出父元素范围的部分会被隐藏 
对应的鸿蒙写法为clip(true) 
示例 
未加clip(true)效果 
Entry
Component
struct Index {build() {Stack() {Column() {Button(按钮).width(350).height(100)}.width(200).height(200).backgroundColor(Color.Red).justifyContent(FlexAlign.Center)}.height(100%).width(100%)}
}加clip(true)效果 
Entry
Component
struct Index {build() {Stack() {Column() {Button(按钮).width(350).height(100)}.width(200).height(200).backgroundColor(Color.Red).justifyContent(FlexAlign.Center).clip(true)}.height(100%).width(100%)}
} 文章转载自: http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.zkzjm.cn.gov.cn.zkzjm.cn http://www.morning.nspzy.cn.gov.cn.nspzy.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.tpnch.cn.gov.cn.tpnch.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.yqgbw.cn.gov.cn.yqgbw.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.gtylt.cn.gov.cn.gtylt.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.ptdzm.cn.gov.cn.ptdzm.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.wyfpc.cn.gov.cn.wyfpc.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.wlxfj.cn.gov.cn.wlxfj.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.wkmpx.cn.gov.cn.wkmpx.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.fesiy.com.gov.cn.fesiy.com