【每日学点HarmonyOS Next知识】清除缓存、弹窗后退手势、读取路由配置文件、相对布局高度自适应、背景颜色透明

1、HarmonyOS 是否有方法可以清除缓存?

是否提供API可以直接清除app的缓存

  1. 当前没有清除缓存的接口,推荐下面:查询缓存用storageStatistics.getCurrentBundleStats()接口。清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存。

详细用法见下面的链接: 查询:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5

  1. 清除不了的数据可能包括首选项和web首选项和Web组件里的缓存,首选项清除请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-preferences-V5#clear

首选项清除操作中,请根据自己的需要使用delete或者clear,具体如下 delete:从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过flush将Preferences实例持久化,使用callback异步回调 clear:清除缓存的Preferences实例中的所有数据,可通过flush将Preferences实例持久化,使用callback异步回调

web清除缓存请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-cookie-and-data-storage-mgmt-V5

2、HarmonyOS 弹窗的后退手势处理问题?

需要实现一套手淘的各个页面均可弹层的功能,涉及到弹层的后退手势处理

希望可以管理各个弹层的后退手势,主动拦截并判定是否响应后退。即允许用户后退手势时,仅退出最顶部的弹窗,也可以允许用户后退手势时,整个页面整体退出。

可以在子页面的根容器NavDestination处调用onbackpressed处理手势拦截,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navdestination-V5#ZH-CN_TOPIC_0000001884757826__onbackpressed10

3、HarmonyOS 如何读取文件router_map.json?

遵照navigation给出的新系统路由注册到profile的router_map.json文件中,希望对此对此router_map做一定的扩展,希望在这个json文件中增加自定义的itemName扩展跳转逻辑,为此我们需要读取router_map文件,把所有itemName和该路由name进行匹配,然而当我使用$r(“app.profile.router_map”)读取router_map是居然不让读,请问有啥办法解决这个问题吗,另外原来的自定义动态路由注册的方案很不好用所以我们打算切换到这个系统路由注册方案,因为自定义太麻烦了

{
  "routerMap": [
  {
    "name": "testPage",
  "pageSourceFile": "src/main/ets/router/TradeTestPage.ets",
  "buildFunction": "getTestPage",
  "data": {
    "description" : "this is PageOne",
    "itemName": "1-21-13-1-3"
  }
  }
  ]
}

目前 route_map.json 暂不支持自定义配置,相关信息请参考:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#%E7%A4%BA%E4%BE%8B2

// 工程配置文件module.json5中配置 {"routerMap": "$profile:route_map"}
// route_map.json
{
  "routerMap": [
  {
    "name": "pageOne",
  "pageSourceFile": "src/main/ets/pages/PageOne.ets",
  "buildFunction": "PageOneBuilder",
  "data": {
    "description": "this is pageOne"
  }
  },
  {
    "name": "pageTwo",
  "pageSourceFile": "src/main/ets/pages/PageTwo.ets",
  "buildFunction": "PageTwoBuilder"
  }
  ]
}
4、HarmonyOS RelativeContainer自适应高度相关?

RelativeContainer不设置height后发现高度是全屏,需要设置什么属性才能自适应子组件的高度?

RelativeContainer支持宽高自适应子组件,将其设置为auto即可,但限制是当width设置auto时,如果水平方向上子组件以容器作为锚点,则auto不生效,垂直方向上同理,具体使用方法请参考文档。文档链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-relativecontainer-V5

请参考以下demo:

@Entry
@Component
struct MyRelativeContainer {
  build() {
    Column() {
      Row() {
        Text('宽度高度自适应')
      }
      .width("100%");
      RelativeContainer() {
        Row()
          .width(100)
          .height(100)
          .backgroundColor("#FF3333")
          .alignRules({})
          .id("row1")
        Row()
          .width(100)
          .height(100)
          .backgroundColor("#FFCC00")
          .alignRules({
            top: { anchor: "row1", align: VerticalAlign.Top },
            left: { anchor: "row1", align: HorizontalAlign.End },
          })
          .id("row2")
        Row()
          .height(100)
          .width(100)
          .backgroundColor("#FF6633")
          .alignRules({
            left: { anchor: "row1", align: HorizontalAlign.Start },
            top: { anchor: "row1", align: VerticalAlign.Bottom }
          })
          .id("row3")
      }
      .width("auto")
      .height("auto")
      .margin({ left: 50 })
      .border({ width: 2, color: "#6699FF" })
    }
    .height('100%')
  }
}
5、HarmonyOS 如何实现将背景颜色设置透明度?

如何实现将背景颜色设置透明度,例如

Text(item.tag).backgroundColor(Color.Gray).fontColor(Color.White)我只想让这个text的背景色具有透明度的效果目前我使用了 .opacity(0.8)来设置,是影响到文字颜色的透明度了

Column() {
  Text("曲阜市三孔旅游区")
    .fontSize("46lpx")
    .fontColor(Color.White)
    .margin({ bottom: "20lpx" })
  Row() {
    Text("5A")
      .fontColor(Color.White)
      .backgroundColor("#DC4237")
      .padding({
        top: "5lpx",
        bottom: "5lpx",
        left: "14lpx",
        right: "14lpx"
      })
      .fontSize("29lpx")
      .borderRadius({
        topRight: "23lpx",
        bottomLeft: "23lpx"
      })
    Text("不负春光踏青而行")
      .fontColor(Color.White)
      .fontSize("29lpx")
      .margin({ left: 10 })
  }
}
.width("100%")
.padding({
  top: "10lpx"
})
.height("150lpx")
.backgroundColor("#333333")
.borderRadius({
  bottomLeft: "58lpx",
  bottomRight: "58lpx"
})
.opacity(0.5)
}

背景色的透明度不用opacity来设置,可以直接使用rgba来进行设置。

// xxx.ets
@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller() private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  @State alpha: string = '' build() { Column() {
  Row() {
    Text('测试内容')
  } .width('100%') // 
  .height(100)
  .backgroundColor(this.alpha) Scroll(this.scroller) {
    Column() {
      ForEach(this.arr, (item) => { Text(item.toString())
        .width('90%') .height(150) .backgroundColor(0xFFFFFF) .borderRadius(15)
        .fontSize(16)
        .textAlign(TextAlign.Center)
        .margin({ top: 10 }) }, item => item) }.width('100%') }
  .width('100%')
  .height('100%')
  .onScroll((xOffset: number, yOffset: number) => { let offset = this.scroller.currentOffset()
    .yOffset
    if (offset >= 150) {
      this.alpha = 'rgba(255,0,0,0.8)'
    } else if (offset <= 0) {
      this.alpha = 'rgba(255,0,0,0.6)'
    } else {
      this.alpha ='rgba(255,0,0,0.3)' }
  })
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

轻口味

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

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

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

打赏作者

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

抵扣说明:

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

余额充值