文档开发者中心
我的

ArkTS一个简单的ui页面

浏览118发布于2025-12-20 01:58江西
本文章最后由 做条懒狗 于 2026-01-06 04:24 编辑

https://developer.huawei.com/consumer/cn/training/classDetail/7e230b074eaa41c587c71c1d1a9a6514?type=1%3Fha_source%3Dhmosclass&ha_sourceId=89000248

效果如图所示:                       cke_3079.png

很多朋友可能刚接触harmony,我给大家带来一个有趣的ui页面,如封面所示。代码如下图所示。可以参考。如想直接使用注意 1.插入的图片,你的media中没有,自己找图片复制粘,位置在 entry-src-main-resources-base-media 
1.滚动主要依靠

收起
自动换行
深色代码主题
复制
List(){
 ListItem(){}
//这里还可以扩充,ListItem(){}
 }
 .scrollBar(BarState.Off)

2.想让屏幕全部变黑不要有上下两边留白,加上这个 

收起
自动换行
深色代码主题
复制
.expendSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])

3.想要合适的间距,多用 .padding  内边距  和 .margin 外边距 ,举个例子

收起
自动换行
深色代码主题
复制
.padding({left:10,bottom: 10,right:10,top:10})
收起
自动换行
深色代码主题
复制
.margin({ left:10,bottom: 10,right:10,top:10 })

这两个也等同于

收起
自动换行
深色代码主题
复制
.padding({10})
收起
自动换行
深色代码主题
复制
.padding({10})

4.如何在两个东西之间插入文字

收起
自动换行
深色代码主题
复制
.layoutWeight(1)//把其他组件排除掉占剩下的组件

就是这个能把其他两个组件排开,自己占剩下的位置!!!

5.要有大局观,先做整体,在优化局部,多尝试,不要害怕报错,慢慢改。

收起
自动换行
深色代码主题
复制
@Entry
@Component
struct Index {
  build() {
    Column() {
      Text("My love")
        .fontSize(24)
        .fontColor(Color.White)
        .width('100%')
        .fontWeight(800)
        .margin({ bottom: 10 })
      List() {
        ListItem(){
          Row(){
            //图
            Image($r('app.media.young'))
              .width(80)
              .border({radius:20})
              .margin({right:10})
            Column(){
              Text("Young")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)

              }
              .width("100%")

            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)

          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.closer'))
              .width(80)
              .height(80)
              .border({radius:20})
              .margin({right:10})
            //文字
            Column(){
              Text("Closer")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)
              }
              .width("100%") //可用于左对齐
            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)
          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.paris'))
              .width(80)
              .border({radius:20})
              .margin({right:10})
            Column(){
              Text("Paris")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)

              }
              .width("100%")

            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)

          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.young'))
              .width(80)
              .border({radius:20})
              .margin({right:10})
            Column(){
              Text("Young")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)

              }
              .width("100%")

            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)

          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.closer'))
              .width(80)
              .height(80)
              .border({radius:20})
              .margin({right:10})
            //文字
            Column(){
              Text("Closer")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)
              }
              .width("100%") //可用于左对齐
            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)
          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.paris'))
              .width(80)
              .border({radius:20})
              .margin({right:10})
            Column(){
              Text("Paris")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)

              }
              .width("100%")

            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)

          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.young'))
              .width(80)
              .border({radius:20})
              .margin({right:10})
            Column(){
              Text("Young")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)

              }
              .width("100%")

            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)

          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.closer'))
              .width(80)
              .height(80)
              .border({radius:20})
              .margin({right:10})
            //文字
            Column(){
              Text("Closer")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)
              }
              .width("100%") //可用于左对齐
            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)
          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
        ListItem(){
          Row(){
            //图
            Image($r('app.media.paris'))
              .width(80)
              .border({radius:20})
              .margin({right:10})
            Column(){
              Text("Paris")
                .fontColor('#F3F3F3')
                .width('100%')
                .fontSize(20)
                .fontWeight(700) //加粗
                .margin(({bottom:10}))
              Row(){
                Text("ViP")
                  .fontColor("#FB9093")
                  .fontWeight(600)
                  .border({width:2,color:'#FB9093',style:BorderStyle.Solid,radius:10})
                  .padding({left:3,right:3,top:3,bottom:3})
                  .margin({right:9})
                Text("The Chainsmokers")
                  .fontColor('#FAFAFA')
                  .fontWeight(600)

              }
              .width("100%")

            }
            .layoutWeight(1)//把其他组件排除掉占剩下的组件
            Image($r("app.media.yuan4"))
              .width(26)

          }
          .width("100%")
          .height(80)
          .backgroundColor(Color.Black)
          .margin({bottom:10})
        }
      }
      .scrollBar(BarState.Off)
      /*可滚动组件:
       List(){
        ListItem(){}


        }
        .scrollBar(BarState.Off)

      */
    }
    .width('100%')
    .height("100%")
    .backgroundColor(Color.Black)
    .padding({left:10,right:10})

    .expandSafeArea([SafeAreaType.SYSTEM],[SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM]) //扩充组件安全区域

  }
}
收藏
0
回复
0
分享
全部评论
暂无评论
暂无评论
写评论
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。
发表