鸿蒙开发入门使用-布局

Row/Column 线性布局

总结:
row 主轴水平对齐方式:justifyContent,交叉轴垂直对齐方式:alignItems
column 主轴垂直对齐方式:justifyContent,交叉轴垂直对齐方式:alignItems
简单说:主轴对齐用justifyContent,交叉轴对齐用alignItems
@Entry
@Component
struct columnRow{
  build() {
    Column({space:10}){
      Row({space:2}){
        Column({space:5}){
          Column().width("80%").height("20%").backgroundColor("#ff17163b")
          Column().width("80%").height("20%").backgroundColor("#ff41a979")
          Column().width("80%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .width("50%").height("15%").backgroundColor("#ffef1717")
        .alignItems(HorizontalAlign.Start) //交叉轴水平左对齐
        Column({space:5}){
          Column().width("80%").height("20%").backgroundColor("#ff17163b")
          Column().width("80%").height("20%").backgroundColor("#ff41a979")
          Column().width("80%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .alignItems(HorizontalAlign.End) //交叉轴水平右对齐
        .width("50%").height("15%").backgroundColor("#ff774444")
      }
      Row({space:2}){
        Column({space:5}){
          Column().width("80%").height("20%").backgroundColor("#ff17163b")
          Column().width("80%").height("20%").backgroundColor("#ff41a979")
          Column().width("80%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .width("50%").height("15%").backgroundColor("#ffef1717")
        .justifyContent(FlexAlign.SpaceBetween) //主轴垂直SpaceBetween
        Column({space:5}){
          Column().width("80%").height("20%").backgroundColor("#ff17163b")
          Column().width("80%").height("20%").backgroundColor("#ff41a979")
          Column().width("80%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .justifyContent(FlexAlign.SpaceAround) //主轴垂直SpaceAround
        .width("50%").height("15%").backgroundColor("#ff774444")
      }
      Row({space:2}){
        Row({space:5}){
          Column().width("20%").height("20%").backgroundColor("#ff17163b")
          Column().width("20%").height("20%").backgroundColor("#ff41a979")
          Column().width("20%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .width("50%").height("15%").backgroundColor("#ffef1717")
        .justifyContent(FlexAlign.SpaceAround)//主轴水平方向SpaceAround
        Row({space:5}){
          Column().width("20%").height("20%").backgroundColor("#ff17163b")
          Column().width("20%").height("20%").backgroundColor("#ff41a979")
          Column().width("20%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .justifyContent(FlexAlign.End) //主轴水平方向右对齐
        .width("50%").height("15%").backgroundColor("#ff774444")
      }

      Row({space:2}){
        Row({space:5}){
          Column().width("20%").height("20%").backgroundColor("#ff17163b")
          Column().width("20%").height("20%").backgroundColor("#ff41a979")
          Column().width("20%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .width("50%").height("15%").backgroundColor("#ffef1717")
        .alignItems(VerticalAlign.Top)//交叉轴垂直方向首行对齐
        Row({space:5}){
          Column().width("20%").height("20%").backgroundColor("#ff17163b")
          Column().width("20%").height("20%").backgroundColor("#ff41a979")
          Column().width("20%").height("20%").backgroundColor("#ff8e2fa4")
        }
        .alignItems(VerticalAlign.Bottom) //交叉轴垂直方向末尾对齐
        .width("50%").height("15%").backgroundColor("#ff774444")
      }
    }
    .width("100%").height("100%")
    .backgroundColor("#ffa26739")

  }
}

Stack 层叠布局

总结: 
Stack中的第一层元素可以进行层叠,通过alignContent设置对齐方式,zIndex设置层级优先级,zIndex值越大,显示层级越高
/**
 * 层叠布局
 * 总结
 * Stack中的第一层元素可以进行层叠,通过alignContent设置对齐方式,zIndex设置层级优先级,zIndex值越大,显示层级越高
 */
let Mtop:Record<string,number> ={"top":50}
@Component
@Entry
struct stackLayout{
  build() {
    Column({space:5}){
      Row(){
        Column(){
          Stack({alignContent:Alignment.TopEnd}){ //方式1 alignContent
            Column().width("90%").height("100%").backgroundColor("#df88a6e7")
            Column().width("60%").height("60%").backgroundColor("#fff")
            Button("1").width("30%").height("30%").backgroundColor("#ff585252")
          }.width("90%").width("90%")
        }.width("45%").height("90%").backgroundColor("#ff8f7676")
        Column(){
          Stack(){
            Column().width("90%").height("100%").backgroundColor("#df88a6e7")
            Column().width("60%").height("60%").backgroundColor("#fff")
            Button("2").width("30%").height("30%").backgroundColor("#ff585252")
          }.width("90%").width("90%").alignContent(Alignment.BottomStart) //方式2 alignContent九种对齐方式
        }.width("45%").height("90%").backgroundColor("#ff8f7676")
      }.width("100%").height("25%").backgroundColor("#fff5cdcd").justifyContent(FlexAlign.SpaceAround)
      Row(){
        Column(){
          Stack(){
            Column().width("90%").height("100%").backgroundColor("#e688aee7").zIndex(3)//zIndex值越大,显示层级越高
            Column().width("60%").height("60%").backgroundColor("#fff").zIndex(2)
            Button("1").width("30%").height("30%").backgroundColor("#ff585252").zIndex(0)
          }.width("90%").width("90%")
        }.width("45%").height("90%").backgroundColor("#ff8f7676")
      }.width("100%").height("25%").backgroundColor("#fff5cdcd")
      Row(){
        Stack({alignContent:Alignment.Bottom}){
          Column({space:20}){
            Row(){
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
            }.justifyContent(FlexAlign.SpaceAround).width("100%")
            Row(){
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
              Column().width("20%").height("20%").backgroundColor("#ff6b6a6a")
            }.justifyContent(FlexAlign.SpaceAround).width("100%")

          }.width("90%").height("100%").backgroundColor("#ffb1b1b1")
          Row(){
            Text("短信")
            Text("联系人")
            Text("设置")
          }.width("80%").backgroundColor("#ff646464").justifyContent(FlexAlign.SpaceBetween)

        }.width("90%").height("100%").backgroundColor("#ffe7e1e1")
      }.width("100%").height("50%").backgroundColor("#fff5cdcd").justifyContent(FlexAlign.Center)
    }
  }
}

Flex 弹性布局

总结:
flex接口以FlexOptions为入参,有以下属性
  direction: 子组件在Flex容器上排列的方向,即主轴的方向
  wrap: Flex容器是单行/列还是多行/列排列。
  justifyContent: 所有子组件在Flex容器主轴上的对齐格式
  alignItems: 所有子组件在Flex容器交叉轴上的对齐格式, wrap为设置为Wrap不生效
  alignContent:交叉轴中有额外的空间时,多行内容的对齐方式。仅在wrap为Wrap或WrapReverse下生效
感觉弹性布局跟ROW和Colum很相似,不同点主要在于warp可以自动换行
* 通用参数:alignSelf,子组件在父容器交叉轴的对齐格式,会覆盖Flex、Column、Row、GridRow布局容器中的alignItems设置
/**
 * 总结:
 * flex接口以FlexOptions为入参,有以下属性
 * direction: 子组件在Flex容器上排列的方向,即主轴的方向
 * wrap: Flex容器是单行/列还是多行/列排列。
 * justifyContent: 所有子组件在Flex容器主轴上的对齐格式
 * alignItems: 所有子组件在Flex容器交叉轴上的对齐格式, wrap为设置为Wrap不生效,使用alignContent
 * alignContent:交叉轴中有额外的空间时,多行内容的对齐方式。仅在wrap为Wrap或WrapReverse下生效
 * 通用参数:alignSelf
 * 子组件在父容器交叉轴的对齐格式,会覆盖Flex、Column、Row、GridRow布局容器中的alignItems设置
 */
@Component
@Entry
struct flex{
  build() {
    Column({space:10}){
      //direction设置主轴方向 默认row
      Flex(){
        Text("1").width("30%").height(50).backgroundColor(0xF5DEB3)
        Text('2').width('30%').height(50).backgroundColor(0xD2B48C)
        Text('3').width('30%').height(50).backgroundColor(0xF5DEB3)
      }
      //row反转
      Flex({direction:FlexDirection.RowReverse}){
        Text("1").width("30%").height(50).backgroundColor(0xF5DEB3)
        Text('2').width('30%').height(50).backgroundColor(0xD2B48C)
        Text('3').width('30%').height(50).backgroundColor(0xF5DEB3)
      }
      //设置主轴方向Column
      Flex({direction:FlexDirection.Column}){
        Text("1").width("30%").height(50).backgroundColor(0xF5DEB3)
        Text('2').width('30%').height(50).backgroundColor(0xD2B48C)
        Text('3').width('30%').height(50).backgroundColor(0xF5DEB3)
      }.height(160)
      //设置主轴方向Column反转
      Flex({direction:FlexDirection.ColumnReverse}){
        Text("1").width("30%").height(50).backgroundColor(0xF5DEB3)
        Text('2').width('30%').height(50).backgroundColor(0xD2B48C)
        Text('3').width('30%').height(50).backgroundColor(0xF5DEB3)
      }.height(150)
      //换行wrap
      Flex({wrap:FlexWrap.Wrap}){
        Text("1").width("50%").height(50).backgroundColor(0xF5DEB3)
        Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
        Text('3').width('50%').height(50).backgroundColor(0xD2B48C)
      }
      //justifyContent主轴对齐方式,alignItems交叉轴对齐方式
      Flex({justifyContent:FlexAlign.SpaceAround,alignItems:ItemAlign.End}){
        Text("1").width("30%").height(30).backgroundColor(0xF5DEB3)
        Text('2').width('30%').height(40).backgroundColor(0xD2B48C)
        Text('3').width('30%').height(50).backgroundColor(0xF5DEB3)
      }
      //子元素的alignSelf属性也可以设置子元素在父容器交叉轴的对齐格式
      Flex(){
        Text('alignSelf Start').alignSelf(ItemAlign.Center).width("20%").height(50).backgroundColor(0xF5DEB3)
        Text('alignSelf Baseline').alignSelf(ItemAlign.End).width('20%').height(50).backgroundColor(0xD2B48C)
        Text('alignSelf Baseline').alignSelf(ItemAlign.Baseline).width('20%').height(80).backgroundColor(0xF5DEB3)
        Text('no alignSelf').alignSelf(ItemAlign.Center).width('25%').height(100)
          .backgroundColor(0xD2B48C)
        Text('no alignSelf').alignSelf(ItemAlign.Baseline).width('25%').height(60)
          .backgroundColor(0xF5DEB3)
      }.width("100%").height(120).backgroundColor(0xAFEEEE)
      //FlexOptions: direction wrap justifyContent alignItems alignContent
      Flex({wrap:FlexWrap.Wrap,justifyContent:FlexAlign.SpaceAround,alignContent:FlexAlign.End}){
        Text('1').width('30%').height(20).backgroundColor(0xF5DEB3)
        Text('2').width('60%').height(20).backgroundColor(0xD2B48C)
        Text('3').width('40%').height(20).backgroundColor(0xD2B48C)
        Text('4').width('30%').height(20).backgroundColor(0xF5DEB3)
        Text('5').width('20%').height(20).backgroundColor(0xD2B48C)
      }.width('90%')
      .height(100)
      .backgroundColor(0xAFEEEE)
    }.height("100%")


  }
}

RelativeContainer 相对布局

/**
 * 总结:
 * 以父容器为锚点
 * align表示父容器的对齐位置在哪里,VerticalAlign(垂直方向对齐),HorizontalAlign(水平方向对齐)
 * anchor表示锚点为哪个,__container__默认为父容器的id
 * 最前面那个bottom、right表示当前容器对齐的位置那哪儿
 */

/**
 * 总结:
 * 以父容器为锚点
 * align表示父容器的对齐位置在哪里,VerticalAlign(垂直方向对齐),HorizontalAlign(水平方向对齐)
 * anchor表示锚点为哪个,__container__默认为父容器的id
 * 最前面那个bottom、right表示当前容器对齐的位置那哪儿
 */
let AlignRus:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>>={
  "bottom":{"anchor":"__container__", 'align': VerticalAlign.Top },
  'right': { 'anchor': '__container__','align': HorizontalAlign.End}
}
@Component
@Entry
struct relativeContainer{

  build() {
    Column({space:10}){
      Column(){
        RelativeContainer(){
          Row().width(100).height(100)
            .backgroundColor("#FF3333")
            .id("row1")
            .alignRules(
              AlignRus
            )
          Row().width(100).height(100)
            .backgroundColor("#FFCC00")
            .id("row2")
            .alignRules({top:{anchor:"row1",align:VerticalAlign.Bottom}})
            .offset({y:10})
        }.width("50%").height("24%").backgroundColor("#ffbebebe").margin(100)
      }
      // Column().width("100%").height("24%").backgroundColor("#ffbebebe")
      // Column().width("100%").height("24%").backgroundColor("#ffbebebe")
      // Column().width("100%").height("24%").backgroundColor("#ffbebebe")
    }.width("100%").height("100%")
  }
}

习题小试-登录页

let fontSize = 14;
@Component
@Entry
struct jd_login {

  build() {
    Column({ space: 20 }) {
      //LOGO
      Column().width(150).height(150).margin({ top: 60 }).backgroundImage($r('app.media.startIcon')).backgroundImageSize(ImageSize.Cover)
      //地区
      Row() {
        Text("国家/地区").fontSize(fontSize)
        Blank()
        Text("中国(+86)").fontSize(fontSize)
        Text() {
          SymbolSpan($r('sys.symbol.chevron_right'))
            .fontWeight(FontWeight.Bold)
            .fontSize(12)
        }
      }
      .width("90%")
      .height("5%")
      .borderRadius(15)
      .backgroundColor("#DCDCDC")
      .padding({ left: 16, right: 10 })
      //账号
      TextInput({placeholder:"请输入手机号"})
        .width("90%")
        .height("5%")
        .borderRadius(15)
        .placeholderFont({size:fontSize})
      //隐私协议
      Row({space:4}){
        Toggle({ type: ToggleType.Checkbox, isOn: false }).width("4%")
        Text(){
          Span("我已阅读同意")
          Span("《隐私政策》《用户协议》").fontColor(Color.Blue)
          Span(",未注册的手机号将自动创建账号")
        }.width("90%").fontSize(12).lineHeight(18).fontColor(Color.Gray)
      }.width("90%").alignItems(VerticalAlign.Top)
      //获取验证码按钮
      Button("获取验证码").width("90%").backgroundColor(Color.Orange).margin({top:24})
      //注册 无法登录
      Row(){
        Text("新用户注册").fontColor(Color.Gray)
        Text("忘记密码").fontColor(Color.Gray)
      }.justifyContent(FlexAlign.SpaceAround).width("90%")
      //其他登录方式
      Text("其他登录方式").fontSize(12).fontColor(Color.Gray).margin({top:100})
      Row(){
        Row({space:4}){
          SymbolGlyph($r('sys.symbol.message_on_message'))
            .fontWeight(FontWeight.Bold)
            .fontSize(12).fontColor([Color.Green])
          Text(){
            Span("微信登录").fontSize(12).border({width:1}).fontColor(Color.Green)
          }
        }.width(80).height(30).justifyContent(FlexAlign.Center).borderRadius(15).backgroundColor("#ffcef8cb")
        Column(){
          SymbolGlyph($r('sys.symbol.envelope'))
            .fontWeight(FontWeight.Bold)
            .fontSize(12).fontColor([Color.Green])
        }.borderWidth(1).width(30).height(30).justifyContent(FlexAlign.Center).borderRadius(30)
        Column(){
          SymbolGlyph($r('sys.symbol.satellite'))
            .fontWeight(FontWeight.Bold)
            .fontSize(12).fontColor([Color.Green])
        }.borderWidth(1).width(30).height(30).justifyContent(FlexAlign.Center).borderRadius(30)
      }.width("60%").height(30).justifyContent(FlexAlign.SpaceAround)
    }
    .width("100%").height("100%").backgroundColor("#fff")
  }
}

GridRow/GridCol 栅格布局

/**
 * 栅格布局:
 * GridRow({ columns: {xs:8,md:4,lg:2}}) 当小屏时展示8列,中屏展示4列,大屏展示2列
 * GridCol({ span: {xs:1,md:2} }) 当小屏时该列占1/8,中屏时该列占2/4
 *
 * breakpoints属性作用是,如:指定xs、md、lg的分辨率范围
 */
/**
 * 栅格布局:
 * GridRow({ columns: {xs:8,md:4,lg:2}}) 当小屏时展示8列,中屏展示4列,大屏展示2列
 * GridCol({ span: {xs:1,md:2} }) 当小屏时该列占1/8,中屏时该列占2/4
 *
 * breakpoints属性作用是,如:指定xs、md、lg的分辨率范围
 */
@Component
@Entry
struct gridRowGridCol {
  @State bgColors: Color[] =
    [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];

  build() {
    GridRow({ columns: {xs:8,md:4,lg:2}}) {
      GridCol({ span: 1 }) {
        Row() {
          Text("1")
        }.width('100%').height('50vp')
      }
      .backgroundColor(Color.Red)
      GridCol({ span: 2 }) {
        Row() {
          Text("2")
        }.width('100%').height('50vp')
      }
      .backgroundColor(Color.Orange)
      GridCol({ span: 1 }) {
        Row() {
          Text("2")
        }.width('100%').height('50vp')
      }
      .backgroundColor(Color.Pink)
      GridCol({ span: 2 }) {
        Row() {
          Text("2")
        }.width('100%').height('50vp')
      }
      .backgroundColor(Color.Yellow)
      GridCol({ span: 2 }) {
        Row() {
          Text("2")
        }.width('100%').height('50vp')
      }
      .backgroundColor(Color.Blue)
    }

  }
}

嵌套使用

@Component
@Entry
struct gridRowGridCol2{
  build() {
    GridRow() {
      GridCol({ span: { sm: 12 } }) {
        GridRow() {
          GridCol({ span: { sm: 2 } }) {
            Row() {
              Text('left').fontSize(24)
            }
            .justifyContent(FlexAlign.Center)
            .height('90%')
          }.backgroundColor('#ff41dbaa')

          GridCol({ span: { sm: 10 } }) {
            Row() {
              Text('right').fontSize(24)
            }
            .justifyContent(FlexAlign.Center)
            .height('90%')
          }.backgroundColor('#ff4168db')
        }
        .backgroundColor('#19000000')
        .height('100%')
      }

      GridCol({ span: { sm: 12 } }) {
        Row() {
          Text('footer').width('100%').textAlign(TextAlign.Center)
        }.width('100%').height('10%').backgroundColor(Color.Pink)
      }
    }.width('100%').height(300)
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值