1、HarmonyOS 像素单位使用哪一个?

UI给的是 750 像素的图,在开发的时候比如一个按钮宽度是 100px /100dp,应该使用 px2vp(100),还是使用 100/2 = 50 vp ,还是其他的?

vp和px互相转换具体可参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-pixel-units-V5#%E5%83%8F%E7%B4%A0%E5%8D%95%E4%BD%8D%E8%BD%AC%E6%8D%A2

接口 描述
vp2px(value : number) : number 将vp单位的数值转换为以px为单位的数值。

说明:

默认使用当前UI实例所在屏幕的虚拟像素比进行转换,UI实例未创建时,使用默认屏幕的虚拟像素比进行转换。
px2vp(value : number) : number 将px单位的数值转换为以vp为单位的数值。

说明:

默认使用当前UI实例所在屏幕的虚拟像素比进行转换,UI实例未创建时,使用默认屏幕的虚拟像素比进行转换。
fp2px(value : number) : number 将fp单位的数值转换为以px为单位的数值。
px2fp(value : number) : number 将px单位的数值转换为以fp为单位的数值。
lpx2px(value : number) : number 将lpx单位的数值转换为以px为单位的数值。
px2lpx(value : number) : number 将px单位的数值转换为以lpx为单位的数值。
2、HarmonyOS 为什么不可以把CustomeDialogController声明在Component之外?

我想要做一个项目通用LoadingDialog,期望在调用的地方非常方便用,例如:globalDialogController.show()

自定义弹窗 CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效, 可以使用promptAction.openCustomDialog来进行弹窗绘制,不使用CustomDialogController , 参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5

3、HarmonyOS List组件不能嵌套Grid组件吗?

List组件内ListItem渲染Grid组件元素无效

尝试如下代码:

@Entry
@Component
struct Index6 {
  build() {
    Row() {
      Column() {
        List() {
          ListItem() {
            Text("我是一个ListItem")
          }.height(200)
          ListItem() {
            Grid() {
              ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (item: number) => {
                GridItem() {
                  Column() {
                    Text(`number:${item}`)
                  }
                  .backgroundColor(`#87${item}`)
                }
              })
            }
            .rowsTemplate('1fr 1fr 1fr 1fr 1fr')
            .columnsTemplate('1fr 1fr 1fr 1fr 1fr')
            .rowsGap(30)
            .columnsGap(20)
            .backgroundColor("#987")
          }.height(200)
        }
      }
      .width('100%')
    }
    .height('100%')
  }
}
4、HarmonyOS 将byte[] 字节流格式的图片数据,展示到Image组件中?

可以通过将buffer转为PixelMap,然后再加载,示例:

let imageSource = image.createImageSource(buffer)
let options = {alphaType: 0,                    // 透明度
  editable: false,                 // 是否可编辑
  pixelFormat: 3,                  // 像素格式
  scaleMode: 1,                    // 缩略值
  size: {height: 100, width: 100}} // 创建图片大小
imageSource.createPixelMap(options).then((pixelMap) => {
  this.image = pixelMap
})
5、HarmonyOS @Builder函数接收的状态变量未引起内部ui变化?

ChangePinPage界面唤起了自定义键盘,在自定义键盘的按钮组件中修改了currentKeyboardType键盘类型来切换中英文键盘,但是CustomKeyboard.ets中Text({\{{$.currentKeyboardType})的值并未变化。

//xxxModel.ets
export class Tmp {
  inputController: TextInputController;
  currentKeyboardType: number;
  constructor(inputController: TextInputController, currentKeyboardType: number) {
    this.inputController = inputController;
    this.currentKeyboardType = currentKeyboardType
  }
}
# ChangePinPage.ets:
@Provide currentKeyboardType: number = Const.KEYBOARD_TYPE_NUMBER; 
...
TextInput()
.customKeyboard(KeyBoardWindow(new Tmp(this.oldPinController, this.currentKeyboardType)))
# CustomKeyboard.ets:
@Builder
export function KeyBoardWindow($$: Tmp) {
  Column() {
    Row() {
      Image($r("app.media.ic_keyboard_down"))// 标题行的”收起“图标
        .onClick(() => {
          $$.inputController.stopEditing()
        })
      Text(`${$$.currentKeyboardType}`)
   }
}
// xxxChild.ets ...
@Consume currentKeyboardType: number;
...
this.currentKeyboardType = 2;

全局@Builder自定义构建函数,采用引用传递,如果放在Navigation()的menus内,状态变量的改变不会触发@Builder方法内的UI刷新

按引用传递参数时,传递的参数可为状态变量,且状态变量的改变会引起@Builder方法内的UI刷新。参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builder-V5

相关demo:

@Builder function ABuilder($$: { paramA1: string }) {
Row() {
Text(`UseStateVarByReference: ${$$.paramA1} `)
}
}
@Entry
@Component
struct Parent {
@State label: string = 'Hello';
build() {
Column() {
// 在Parent组件中调用ABuilder的时候,将this.label引用传递给ABuilder
ABuilder({ paramA1: this.label })
Button('Click me').onClick(() => {
// 点击“Click me”后,UI从“Hello”刷新为“ArkUI”
this.label = 'ArkUI';
})
}
}
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐