20分钟鸿蒙应用实战开发:百度移动端首页

鸿蒙应用开发作为时下兴起,且对前端友好的技术,掌握鸿蒙开发可以多一条技术发展的道路,也可以是前端职业发展的一条后备之路。

本篇旨在通过鸿蒙开发百度搜索移动端首页,帮助大家快速了解鸿蒙应用的基本开发流程,愉快地走上鸿蒙开发之旅。

开发之前

开发之前我们需要对鸿蒙技术有所了解,可以先通过鸿蒙官方文档建立一个全面的认识,并且本篇不会对技术细节做过多的讲解,毕竟术业有专攻。

其次我们可以先来看看百度移动端首页,了解下我们要开发的页面。

创建项目

安装好 DevEco Studio 鸿蒙专用编辑器之后,点击"Create Project",再点击 Empty Ability->Next->Finish,即可初始化一个鸿蒙应用。

初始化后的项目结构如下,页面代码在图中的 Index.ets 中开发,点击右侧的 Previewer,即可实时预览运行效果,保存代码时也会自动预览。

编写代码

页面结构

在实现一个页面之前,我们先来梳理页面布局结构,在鸿蒙应用中,build{} 里面是我们自己写的页面代码,此外的代码是固定的配置。

build 里面的第一层是一个 Column,表示是一种垂直的布局方式,如果是水平布局,就可以使用 Row。Column 里的第一层是 List,List 主要是能够提供滚动的能力,并且与 ListItem 固定搭配。这里可以把顶部的菜单、Logo、搜索框划放到第一个 ListItem里,下方的新闻列表放到第二个 ListItem 里,方便设置公共样式。

我们还可以看到鸿蒙里的标签、样式都与 H5 有所不同,这有点类似前端框架里的 JSX 写法,也意味着从前端开发过渡到鸿蒙开发是有一定的成本的。

顶部区域实现

顶部菜单主要通过 Row + justifyContent('Spacebetween') 实现布局,这个很像是 CSS 里面的 flex 布局,只是换了个形式。其余部分中,图片使用 Image 组件,输入框使用 TextInput 组件,按钮使用 Button 组件,再通过一些样式即完成了顶部区域的实现。

新闻列表实现

可以看到,纯文字新闻比较简单,使用 Column 组件,加上两个 Text 组件即可;图文新闻主要用上 Flex 布局,加上些 Image 组件,每一个独立的模块都可以用 Column 、Row 或 Flex 组件包裹。

运行效果

源码

下面贴上这个百度移动端页面的源码,感兴趣的同学可以自行调试下,注意图片地址需要自行替换。

typescript 复制代码
@Entry
@Component
struct Baidu {
  build() {
    Column() {
      List() {
        ListItem() {
          Column() {
            // 顶部菜单
            Row() {
              Image($r('app.media.10031'))
                .width(25)
              Image($r('app.media.10026'))
                .width(25)
            }
            .width("100%")
            .justifyContent(FlexAlign.SpaceBetween)
            .margin({
              top: 5
            })

            //百度logo
            Image($r('app.media.10001'))
              .width(170)

            // 输入框和搜索按钮
            Row() {
              TextInput({
                placeholder: '输入搜索词'
              })
                .fontColor("#8585a5")
                .height(45)
                .padding({ left: 10 })
                .border({
                  width: 1,
                  color: "#4e6ef2"
                })
                .borderRadius({
                  topLeft: 10,
                  bottomLeft: 10
                })
                .layoutWeight(1)

              Button("百度一下", { type: ButtonType.Normal })
                .width(100)
                .height(45)
                .backgroundColor("#4e6ef2")
                .fontColor(Color.White)
                .borderRadius({
                  topRight: 10,
                  bottomRight: 10
                })
            }
            .width("100%")
          }
          .height(180)
          .justifyContent(FlexAlign.SpaceBetween)
        }

        ListItem() {
          Column() {
            // 纯文字新闻
            Column() {
              Text("事关强国强军,习主席发出新号令")
                .width("100%")
                .margin({
                  bottom: 10
                })
              Text("新华网")
                .fontColor("#b1bddb")
                .fontSize(12)
                .width("100%")
                .margin({
                  bottom: 10
                })
            }
            .width("100%")

            Column() {
              Text("习近平谈生态:风物长宜放眼量")
                .width("100%")
                .margin({
                  bottom: 10
                })
              Text("中国新闻网")
                .fontColor("#b1bddb")
                .fontSize(12)
                .width("100%")
                .margin({
                  bottom: 10
                })
            }
            .width("100%")

            Column() {
              Text("习近平激励全军奋力开创强军事业新局面")
                .width("100%")
                .margin({
                  bottom: 10
                })
              Text("新华网")
                .fontColor("#b1bddb")
                .fontSize(12)
                .width("100%")
                .margin({
                  bottom: 10
                })
            }
            .width("100%")
            .border({
              width: {
                bottom: 1
              },
              color: 'rgba(128, 128, 128,0.3)'
            })

            // 图文新闻
            Column() {
              Text("AI东风再起,Sora大模型横空出世对原创动漫创作的影响")
                .width("100%")
              Flex({
                justifyContent: FlexAlign.SpaceBetween
              }) {
                Image($r('app.media.10003'))
                  .width(112)
                  .borderRadius({
                    topLeft: 10,
                    bottomRight: 10
                  })
                Image($r('app.media.10004'))
                  .width(112)
                Image($r('app.media.10005'))
                  .width(112)
                  .borderRadius({
                    topRight: 10,
                    bottomLeft: 10
                  })
              }
              .width("100%")

              Row() {
                Text("新闻晨报")
                  .fontColor("#b1bddb")
                  .fontSize(12)
                Image($r('app.media.10038'))
                  .width(10)

              }
              .width("100%")
              .justifyContent(FlexAlign.SpaceBetween)

            }
            .justifyContent(FlexAlign.SpaceEvenly)
            .height(160)
            .border({
              width: {
                bottom: 1
              },
              color: {
                bottom: 'rgba(128, 128, 128,0.3)'
              }

            })

            Column() {
              Text("'这个缠绕香港26年多的问题',终于要解决了!")
                .width("100%")
              Flex({
                justifyContent: FlexAlign.SpaceBetween
              }) {
                Image($r('app.media.10006'))
                  .width(112)
                  .borderRadius({
                    topLeft: 10,
                    bottomRight: 10
                  })
                Image($r('app.media.10007'))
                  .width(112)
                Image($r('app.media.10008'))
                  .width(112)
                  .borderRadius({
                    topRight: 10,
                    bottomLeft: 10
                  })
              }
              .width("100%")

              Row() {
                Text("大皖新闻")
                  .fontColor("#b1bddb")
                  .fontSize(12)
                Image($r('app.media.10038'))
                  .width(10)

              }
              .width("100%")
              .justifyContent(FlexAlign.SpaceBetween)

            }
            .justifyContent(FlexAlign.SpaceEvenly)
            .height(160)
            .border({
              width: {
                bottom: 1
              },
              color: {
                bottom: 'rgba(128, 128, 128,0.3)'
              }

            })

            Column() {
              Text("现场直击!新晋武器控制师首飞拉开战幕")
                .width("100%")
              Flex({
                justifyContent: FlexAlign.SpaceBetween
              }) {
                Image($r('app.media.10009'))
                  .width(112)
                  .borderRadius({
                    topLeft: 10,
                    bottomRight: 10
                  })
                Image($r('app.media.10010'))
                  .width(112)
                Image($r('app.media.10011'))
                  .width(112)
                  .borderRadius({
                    topRight: 10,
                    bottomLeft: 10
                  })
              }
              .width("100%")

              Row() {
                Text("鲁网")
                  .fontColor("#b1bddb")
                  .fontSize(12)
                Image($r('app.media.10038'))
                  .width(10)

              }
              .width("100%")
              .justifyContent(FlexAlign.SpaceBetween)

            }
            .justifyContent(FlexAlign.SpaceEvenly)
            .height(160)
            .border({
              width: {
                bottom: 1
              },
              color: {
                bottom: 'rgba(128, 128, 128,0.3)'
              }
            })
          }
          .margin({
            top: 30
          })
        }
      }
      .scrollBar(BarState.On)
      .padding({
        top: 10,
        right: 10,
        left: 10
      })
    }
  }
}

总结

以上是鸿蒙应用开发(Stage 模型)的实战例子,写法与前端开发很像,但又有所差异。相信我们可以借助前端开发的经验,多学多练,掌握了鸿蒙应用开发,也就多掌握了一种就业技能。

相关推荐
强强学习1 小时前
HTML5 起步
前端·html·html5
念九_ysl3 小时前
前端循环全解析:JS/ES/TS 循环写法与实战示例
前端·javascript·typescript
anyup_前端梦工厂5 小时前
了解几个 HTML 标签属性,实现优化页面加载性能
前端·html
前端御书房5 小时前
前端PDF转图片技术调研实战指南:从踩坑到高可用方案的深度解析
前端·javascript
2301_789169545 小时前
angular中使用animation.css实现翻转展示卡片正反两面效果
前端·css·angular.js
Huang兄6 小时前
鸿蒙-状态管理V1
华为·harmonyos
风口上的猪20156 小时前
thingboard告警信息格式美化
java·服务器·前端
程序员黄同学6 小时前
请谈谈 Vue 中的响应式原理,如何实现?
前端·javascript·vue.js
爱编程的小庄7 小时前
web网络安全:SQL 注入攻击
前端·sql·web安全