HarmonyOS button文字怎么自定义设置位置?

以下button,内包含图片和文字,现在需求是图片后间隔5vp跟着文字。 backgroundImagePosition({ x:10, y:7}) 图片的可以自定义位置,请问一下,怎么样自定义文字的文字

Button('搜索应用').borderRadius('50%').backgroundColor(0xF2F2F2).fontColor(0xBFBFBF)
  .backgroundImage($r('app.media.ProduceSearch'))
  .backgroundImageSize({width:25,height:25}).backgroundImagePosition({ x:10, y:7})
  .alignRules({
    center: { anchor: '__container__', align: VerticalAlign.Center },
    right: { anchor: 'applicationCenter', align: HorizontalAlign.Start },
    left: { anchor: 'title', align: HorizontalAlign.End }
  }).margin(10).layoutWeight(1).align(Alignment.Center)
阅读 557
1 个回答

当前button里没有字体具体的位置的设置,可以通过align和padding达到想要的效果,backgroundImagePosition不占空间,可以设置align想左对齐,再设置padding的left相当于图片width+backgroundImagePosition的x+5vp即可

Button('搜索应用')
  .backgroundColor(0xF2F2F2)
  .fontColor(0xBFBFBF)

  .backgroundImage($r('app.media.ProduceSearch'))
  .backgroundImageSize({width:25,height:25})
  .backgroundImagePosition({ x:20, y:12.5})
    //
  .alignRules({
    center: { anchor: '__container__', align: VerticalAlign.Center },
    right: { anchor: 'applicationCenter', align: HorizontalAlign.Start },
    left: { anchor: 'title', align: HorizontalAlign.End }
  })
  .margin(10)
  .align(Alignment.Start)
  .width(300)
  .height(50)
  .padding({ left: 25 + 20 + 5 })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进