首页 > 其他分享 >重生之我要当前端大王–鸿蒙next篇 02 常用布局组件

重生之我要当前端大王–鸿蒙next篇 02 常用布局组件

时间:2024-09-15 10:52:24浏览次数:3  
标签:02 count 鸿蒙 url media app height next 100%

重生之我要当前端大王–鸿蒙next篇 02 常用布局组件

第三篇章 鸿蒙next星河版

前言

阅读本章,学习Flex布局、Grid网格布局、Stack层叠布局

一、Flex布局

和html的flex相识,arkUi的flex组件也分为主轴和副轴,并提供声明式api进行设置排布样式,让我们来学习一下

@Entry
@Component
struct Flexs {
  build() {
    Column() {
      Text('鸿蒙next').fontSize(30).fontWeight(FontWeight.Bold).width('100%')
    Flex({
      wrap:FlexWrap.Wrap,//换行
      direction:FlexDirection.Row,//设置主轴
      justifyContent:FlexAlign.Start,//设置主轴元素排布
      alignItems:ItemAlign.Center//设置副轴元素排布
    }){
      Text('ArkUI').isText()
      Text('ArkTs').isText()
      Text('界面开发').isText()
      Text('系统能力').isText()
      Text('权限控制').isText()
      Text('元服务').isText()
    }
    }
  }
}
@Extend(Text)
function isText(){
  .fontSize(20)
  .padding(15)
  .backgroundColor('#f1f1f1')
  .margin(5)
}

样式效果:
在这里插入图片描述

二、Grid网格布局

学习了解一下语法使用

@Entry
@Component
struct GridPage {
  build() {
    Column() {
      Grid(){
      ForEach([1,2,3,4,5,6],(item:number)=>{
      GridItem(){
        Text(item.toString())
      }.backgroundColor(Color.Pink)
      })
      }
      .columnsTemplate('1fr 1fr 1fr')//三列
      .rowsTemplate('1fr 1fr')//俩行
      .columnsGap(10)//列间距
      .rowsGap(10)//行间距
      .width('100%')
      .height(300)
    }
    .height('100%')
    .width('100%')
  }
}

页面效果:
在这里插入图片描述

三、Stack层叠布局

使用Stack,元素往下的排在上层

@Entry
@Component
struct Stacks {

  build() {
    Column() {
    Column(){
      Stack({alignContent:Alignment.Bottom}){//设置元素排布位置
        Image($r('app.media.stack')).borderRadius({topLeft:15,topRight:15})
        Row(){
          Row({space:10}){
            Image($r('app.media.bz_play')).height(25).fillColor(Color.White)
            Text('288万').fontColor(Color.White)
          }.margin({right:10})
          Row({space:10}){
            Image($r('app.media.bz_msg')).height(20).fillColor(Color.White)
            Text('288万').fontColor(Color.White)
          }
          Blank()
          Text('4:33').fontColor(Color.White)
        }
        .padding({left:10,right:5})
        .justifyContent(FlexAlign.SpaceBetween)
        .alignItems(VerticalAlign.Center)
        .width('100%')
      }.width('100%')
      .height(200)
      .backgroundColor(Color.Gray)
      Column(){}
      Text('【凤凰传奇新歌】欢迎来到国风统治区:唢呐一响神曲《铁衣流派推广曲》')
        .fontSize(18)
        .maxLines(2)
        .textOverflow({overflow:TextOverflow.Ellipsis})
        .margin({top:10})

      Row(){
        Text('19万点赞').width(70).height(25).backgroundColor(Color.Orange).fontSize(16)
        Image($r('app.media.bz_more')).height(20)
      }
      .justifyContent(FlexAlign.SpaceBetween)
      .width('100%')
      .padding(10)
    }
    .height(300)
    .backgroundColor(Color.White)
    .borderRadius({bottomLeft:10,bottomRight:10})
    }
    .height('100%')
    .width('100%')
    .backgroundColor(Color.Gray)
    .padding(20)
  }
}

页面效果:
在这里插入图片描述

最后使用这些布局做个集生肖案例,点击抽取生肖,抽取随机生肖卡,筹集6个生肖卡,获得奖品,点击再来一次,即可重新抽卡
页面效果:在这里插入图片描述
源码:

interface  cardList{
  url:string,
  count:number
}
let cpCardList:cardList[]=[
  {url:'app.media.bg_00',count:0},
  {url:'app.media.bg_01',count:0},
  {url:'app.media.bg_02',count:0},
  {url:'app.media.bg_03',count:0},
  {url:'app.media.bg_04',count:0},
  {url:'app.media.bg_05',count:0}
]
@Entry
@Component
struct AnimalsignPage {
  @State cardList: cardList[] = [
    {url:'app.media.bg_00',count:0},
    {url:'app.media.bg_01',count:0},
    {url:'app.media.bg_02',count:0},
    {url:'app.media.bg_03',count:0},
    {url:'app.media.bg_04',count:0},
    {url:'app.media.bg_05',count:0}
  ]
  @State  maskOpacity:number=0
  @State  maskZindex:number =-1
  @State maskImageX:number=0
  @State maskImageY:number=0
  @State prizeImageX:number=0
  @State  prizeImageY:number=0
 @State  randomNum:number=0
  @State prizeZindex:number=-2
  @State prizeOpacity:number=0

  build() {
    Stack(){
      Column(){
        Grid(){
          ForEach(this.cardList,(item:cardList)=>{
            GridItem(){
              Badge({
                count:item.count,
                position:BadgePosition.RightTop,
                style:{
                  badgeSize:20,
                  fontSize:16
                }
              }){
                Image($r(item.url)).height(120)
              }
            }
          })
        }.columnsTemplate('1fr 1fr 1fr ')
        .rowsTemplate('1fr 1fr')
        .height(300)

        Button('立即抽卡')
          .width(300)
          .backgroundColor('#ed5b8c')
          .margin({top:35})
          .onClick(()=>{
            this.maskOpacity=1
            this.maskZindex=99
            this.maskImageX=1
            this.maskImageY=1
            this.randomNum=Math.floor(Math.random()*6)
          })
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
      .zIndex(10)

        Column({space:10}){
        Text('获得生肖卡').fontSize(24).fontColor('#f5ebcf')
          Image($r(`app.media.img_0${this.randomNum}`))
            .height(180)
            .scale({
              x:this.maskImageX,
              y:this.maskOpacity
            })
            .animation({
              duration:500
            })
          Button('开心收下')
            .fontColor('#f5ebcf')
            .backgroundColor(Color.Transparent)
            .border({width:2,color:'#f5ebcf'})
            .height(40)
            .onClick(()=>{
              this.maskOpacity=-1
              this.maskZindex=0
              this.maskImageX=0
              this.maskImageY=0
              let newCardList=[...this.cardList]
              newCardList[this.randomNum].count++
              newCardList[this.randomNum].url=`app.media.img_0${this.randomNum}`
              this.cardList=newCardList
           let code= this.cardList.every((item)=>{
             return   item.count>=1
              })
              if (code) {
                this.prizeZindex=99
                this.prizeOpacity=1
                this.prizeImageX=1
                this.prizeImageY=1
              }
            })
        }
        .height('100%')
        .width('100%')
        .backgroundColor('#cc000000')
        .justifyContent(FlexAlign.Center)
        .opacity(this.maskOpacity)
        .zIndex(this.maskZindex)
        .animation({
          duration:500
        })
        Column({space:10}){
          Image($r('app.media.mz'))
            .height(300)
            .scale({
              x:this.prizeImageX,
              y:this.prizeImageY
            }).animation({
            duration:1000
          })
          Button('再来一次')
            .backgroundColor(Color.Transparent)
            .border({width:2,color:Color.White})
            .width(220)
            .onClick(()=>{
              this.prizeZindex=-2
              this.prizeOpacity=0
              this.cardList=cpCardList
            })
        }
        .height('100%')
        .width('100%')
        .backgroundColor('#cc000000')
        .justifyContent(FlexAlign.Center)
        .opacity(this.prizeOpacity)
        .zIndex(this.prizeZindex)
        .animation({
          duration:1000
        })

    }

  }
}

总结

以上就是今天要讲的内容,本文介绍了常用的三种布局组件,文章仅用于学习交流,文章内容到此结束,你的点赞是我更新的动力。

标签:02,count,鸿蒙,url,media,app,height,next,100%
From: https://blog.csdn.net/zzy831153/article/details/142267779

相关文章

  • 【2024研赛】【华为杯】2024 年研究生数学建模比赛思路、代码更新中.....
    ......
  • 2024年金典Java面试八股文
    1、什么是自动拆装箱 int和Integer有什么区别   难度系数:⭐基本数据类型,如int,float,double,boolean,char,byte,不具备对象的特征,不能调用方法。装箱:将基本类型转换成包装类对象拆箱:将包装类对象转换成基本类型的值java为什么要引入自动装箱和拆箱的功能?主要是用于jav......
  • 鸿蒙应用开发快速学习指南(初级篇-4 应用程序框架基础)
    应用程序框架基础第四课应用程序框架基础在上一课结束后,补足了声明式UI部分的知识之后,依旧是先看习题。判断题在基于Stage模型开发的应用项目中都存在一个app.json5配置文件、以及一个或多个module.json5配置文件。正确(True)错误(False)首先问题中提到了Stage模型,什......
  • D02【python接口自动化学习】-python基础
    day02字符串(上)学习日期:0909学习目标:字符串(上):python是如何处理单词的?学习笔记:字符串的常用方法#字符串常用方法#打印字符串的个数print('xyxyxyz'.count('x'))#输出3print('xyxyxyz'.count('xy'))#输出3print('xyxyxyz'.count('a'))#输......
  • 2024.09.14小红书
    1.小红的文章匹配小红书的第i篇文章有一个点赞数ai。小红认为,如果两篇不同的文章满足:点赞数通过位异或运算恰好得到k,那么这两篇文章是相似文章,即aixoraj=k。现在小红收集到了n篇文章的点赞数,请帮助她计算出有多少对(i,j)是相似文章。输入描述第一行输入两个整数n......
  • 【专题】2024新能源企业“出海”系列之驶向中东、东南亚报告合集PDF分享(附原数据表)
    原文链接: https://tecdat.cn/?p=37698在“双碳”目标引领下,中国新能源产业近年迅猛发展,新能源企业凭借技术革新、政策支持与市场驱动实现快速增长,在产业链完备、技术领先、生产效能及成本控制等方面优势显著。面对国内外环境不确定性增强的常态化态势,中国新能源企业积极开拓海外......
  • 2024.9.14
    DATE#:202409014ITEM#:DOCWEEK#:SATURDAYDAIL#:捌月拾贰TAGS<BGM="诀别无尽夏--YouzeeMusic"><theme=oi-contest><[NULL]><[空]><[空]>“每个夏天的句号都是窗外要烂掉的绿”A.上海时间限制:1s 内存限制:512MB 测评类型:传......
  • 2024.9.14
    今日总结:1:约数个数和这道题主要是一道数学题,主要的推到过程需要用到莫比乌斯反演,但是在求第一步用分块处理出1~n内的f(i)的值,再用线性筛求出u(i)的值和他的前缀和,我卡在了最后一步对原式进行数论分块点击查看代码#include<bits/stdc++.h>usingnamespacestd;constint......
  • 【2025】“急救课堂”微信小程序的设计与实现, 在线急救培训与模拟演练、在线急救教育
    博主介绍:  ✌我是阿龙,一名专注于Java技术领域的程序员,全网拥有10W+粉丝。作为CSDN特邀作者、博客专家、新星计划导师,我在计算机毕业设计开发方面积累了丰富的经验。同时,我也是掘金、华为云、阿里云、InfoQ等平台的优质作者。通过长期分享和实战指导,我致力于帮助更多学生......
  • 关于鸿蒙开发框架,页面搭建样式语法总结
    鸿蒙中的变量/常量创建采用了ts语法我们在声明变量的同时需要指定变量类型,定义变量时也是要遵守变量命名规范:    1.只能包含数字、字母、下划线、$,不能以数字开头…重点    2.不能使用内置关键字或保留字,比如let、const    3.严格区分大小写1.变......