HarmonyOS鸿蒙NEXT中病虫害详细信息页面布局build实现
HarmonyOS鸿蒙NEXT中病虫害详细信息页面布局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;
}
更多关于HarmonyOS鸿蒙NEXT中病虫害详细信息页面布局build实现的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS鸿蒙NEXT中病虫害详细信息页面布局build实现的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS NEXT中实现病虫害详细信息页面布局,使用ArkUI的声明式开发范式。主要构建结构如下:
- 使用
Column
作为根容器,设置width('100%')
和padding
; - 顶部用
Image
组件展示病虫害图片,设置objectFit: ImageFit.Contain
; - 中间使用
Text
组件显示病虫害名称,设置fontSize
和fontWeight
; - 详细描述区域用
Text
配合Flex
布局,设置textAlign: TextAlign.Start
; - 底部防治措施使用
ForEach
循环渲染防治条目。
关键代码示例:
build() {
Column() {
Image($r('app.media.pest_image'))
Text(this.pestInfo.name)
Text(this.pestInfo.description)
ForEach(this.pestInfo.controls, item => {
Text(item.method)
})
}
}
使用@State
装饰器管理页面状态数据。
从代码实现来看,这个HarmonyOS Next的病虫害详情页布局设计合理,主要实现了以下功能点:
-
页面采用Column嵌套结构,上半部分展示图片(占50%高度),下半部分展示详细信息
-
图片区域通过ImageFit.Contain保持比例自适应,并添加点击返回功能
-
详细信息区域包含:
- 农作物和病虫害名称标题(24px中号字体)
- "防治方法"子标题(18px中号字体)
- 分割线区分内容区域
- 可滚动的防治方法文本内容(14px常规字体)
-
通过display系统能力获取设备宽度,使图片能满屏显示
-
根据pestObj.PestName判断显示详情页还是诊断页
几点优化建议:
- 图片区域可以添加加载状态提示
- 防治方法文本可考虑分段显示
- 可添加收藏/分享等操作按钮
整体实现符合HarmonyOS的声明式UI开发规范,布局层次清晰,功能完整。