#放码来战.端云一体化开发#HarmonyOS 5 【农民叔叔】17.【端侧工程】病虫害详细信息页面布局build实现

点击首页病虫害图集列表项,跳转至具体病虫害的详细信息页,内容包括:病虫害图片、农作物品名、病虫害名称、防治办法等。

布局结构如下图所示:

在PestInfoPage.ets文件中自定义病虫害详细信息视图组件PestInfoView,代码如下:

/**
 * 病虫害详细信息视图
 */
@Component
struct PestInfoView{
  private pestObj?:PestModel;
  private imgWidth?:number;

  build() {
    Column(){
      // 病害图片
      Column(){
        Image(this.pestObj?.Img)
          .width(this.imgWidth)
          .objectFit(ImageFit.Contain)
          .onClick(() => {
            router.back();
          })
      }
      .height('50%')
      .backgroundColor($r('sys.color.ohos_id_color_sub_background'))

      // 详细信息
      Column(){
        // 农作物名-病虫害名称
        Text(this.pestObj?.Type + '-' + this.pestObj?.PestName)
          .fontSize(24)
          .fontWeight(FontWeight.Medium)
          .margin({top:12})
          .fontColor($r('sys.color.ohos_id_color_text_primary'))

        // 标题
        Text('防治方法')
          .fontSize(18)
          .fontWeight(FontWeight.Medium)
          .margin({top:6})
          .fontColor($r('sys.color.ohos_id_color_text_secondary'))

        // 分割线
        Divider()
          .color($r('sys.color.ohos_id_color_card_bg'))
          .borderWidth(0.25)
          .margin({top:4,bottom:4})

        // 具体防治描述
        Scroll(){
          Row(){
            Text((this.pestObj?.PestCtrl !==undefined && this.pestObj?.PestCtrl?.toString()!='')
              ? this.pestObj?.PestCtrl
              : '暂无内容')
              .lineHeight(24)
              .fontSize(14)
              .fontColor($r('sys.color.ohos_id_color_text_secondary'))
          }
          .justifyContent(FlexAlign.Start)
        }
        .scrollBar(BarState.Off)
        .layoutWeight(1)
        .align(Alignment.TopStart)
      }
      .width('100%')
      .layoutWeight(1)
      .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
      .padding({left:12,right:12})
    }
  }
}

病虫害信息页面加载时,根据病虫害实体对象的属性pestname病虫名称是否空,判断是新的诊断,还是已有的病虫害,对应显示AI诊断视图或信息视图。

build() {
    Column(){
      if(!DataUtil.isNull(this.pestObj.PestName)){
        // 已有病虫害详情
        PestInfoView({pestObj:this.pestObj,imgWidth:this.imgWidth});
      }else {
        // 新增病虫害图片,需诊断
        PestDiagnosisView({pestObj:this.pestObj,imgWidth:this.imgWidth});
      }
    }
  }

为了宽度满屏显示病虫害图片,需要用到系统能力的设备屏幕属性display,获取当前设备的屏幕宽度,并赋值给Image.width

@State deviceWidth:number =Constants.DEVICE_DEFAULT_WIDTH;
  @State imgWidth:number =this.deviceWidth;

  aboutToAppear(): void {
    let displayClass:display.Display=display.getDefaultDisplaySync();
    let width=displayClass?.width/displayClass.densityPixels ?? Constants.DEVICE_DEFAULT_WIDTH;
    this.deviceWidth=width;
    this.imgWidth =this.deviceWidth;
  }

屏幕属性可参加官方资料:

文档中心

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸿花粉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值