HarmonyOS Developer之五星好评

rating

rating为评分条组件,用来实现用户使用感受的衡量标准条(如:五星好评)

在pages/index目录下的hml文件中创建一个rating组件:

HTML

html 复制代码
<!-- xxx.hml -->
<div class="container">
  <rating></rating>
</div>

CSS

css 复制代码
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
rating {
  width: 80%;
  height: 150px;
}

实现非5星好评

rating组件通过设置numstars和rating属性设置评分条的星级总数和当前评星数:

HTML:

html 复制代码
<!-- xxx.hml -->
<div class="container">
  <rating numstars="6" rating="5">
  </rating>
</div>
css 复制代码
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
rating {
  width: 80%;
  height: 150px;
}

自定义评分样式

rating组件通过star-background、star-foreground和star-secondary属性设置单个星级未选择、选中和选中的次级背景图片:

HTML

html 复制代码
<!-- xxx.hml -->
<div class="container">
  <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;;">
    <rating numstars="5" rating="1" class="myrating" style="width: {{ratewidth}}; height:{{rateheight}}; star-background: {{backstar}}; star-secondary: {{secstar}};star-foreground: {{forestar}};rtl-flip: true;">
    </rating>
  </div>
</div>

CSS

css 复制代码
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}

JS

javascript 复制代码
// index.js
export default {
  data: {
    backstar: 'common/love.png',
    secstar: 'common/love.png',
    forestar: 'common/love1.png',
    ratewidth: '400px',
    rateheight: '150px'
  },
  onInit(){
  }
}
复制代码
说明
 - star-background、star-secondary、star-foreground属性的星级图源必须全部设置,否则默认的星级颜色为灰色,提示图源设置错误。
 - star-background、star-secondary、star-foreground属性只支持本地路径图片,图片格式为png和jpg。
相关推荐
aqi0027 分钟前
一文读懂 HarmonyOS 7.0 带来的十大API重要升级
android·华为·harmonyos·鸿蒙·harmony
贾伟康36 分钟前
【HarmonyOS 7新能力|037】数字盾工程封装:把接入逻辑放进可维护的分层结构
安全·harmonyos·arkts·软件架构·数字签名
ai安歌1 小时前
不调用外部 typst 命令:Draftmark 用 FRB 内嵌排版引擎的实现
harmonyos
威哥爱编程2 小时前
HarmonyOS 首开提速实战:首屏白屏的三种归因与对症方案
harmonyos·arkts
威哥爱编程2 小时前
HarmonyOS 折叠屏与鸿蒙电脑适配:布局一崩,八成是断点没定义
华为·harmonyos·arkts
威哥爱编程2 小时前
HarmonyOS 穿戴独立 UX 实战:三道坎、两个降级、一张自检清单
华为·harmonyos·arkts
贾伟康2 小时前
【HarmonyOS 7新能力|038】游戏快启工程封装:把接入逻辑放进可维护的分层结构
性能优化·harmonyos·arkts·游戏开发·软件架构
威哥爱编程2 小时前
HarmonyOS LTPO 帧率实战:别把刷新率锁死 120Hz,expected 按内容填
harmonyos·arkts
威哥爱编程2 小时前
HarmonyOS 7 Agent A2A 实战:让日程智能体和打车智能体自己谈成一单
华为·harmonyos·arkts
威哥爱编程2 小时前
HarmonyOS 跨设备互通实战:平板点一下,手机镜头帮你拍照
华为·harmonyos·arkts