文档管理中心
FAQ应用框架开发UI框架组件使用Search组件是否支持设置placeholder文本和搜索图标的间距

Search组件是否支持设置placeholder文本和搜索图标的间距

问题现象

无法设置Search组件的placeholder文本和搜索图标的间距,应该如何实现?

效果预览

背景知识

Search组件支持设置placeholder文本颜色placeholder文本样式,包括字体大小,字体粗细,字体族,字体风格。暂不支持设置placeholder文本和搜索图标的间距。

解决方案

Search组件不支持设置placeholder文本和搜索图标的间距,可以自定义搜索框组件实现该效果:

收起
自动换行
深色代码主题
复制
  1. @Entry
  2. @Component
  3. struct searchTest {
  4. @State searchText: string = '';
  5. build() {
  6. Row() {
  7. Column({ space: 10 }) {
  8. Row({ space: 5 }) {
  9. Row() {
  10. Image($r('sys.media.ohos_ic_public_search_filled'))
  11. .size({ width: 18, height: 18 })
  12. .opacity(0.5)
  13. .margin({ left: 10, right: 0 })
  14. TextInput({ placeholder: '搜索', text: this.searchText })
  15. .padding({ left: 4 })
  16. .layoutWeight(1)
  17. .backgroundColor(Color.Transparent)
  18. .height('100%')
  19. }
  20. .layoutWeight(1)
  21. .backgroundColor('#f2f3f5')
  22. .borderRadius('50%')
  23. }.width('100%')
  24. .height(40)
  25. }
  26. .width('100%')
  27. .padding({ top: 20, left: 20, right: 20 })
  28. .height('100%')
  29. }
  30. .height('100%')
  31. .backgroundColor(Color.White)
  32. }
  33. }

常见FAQ

Q:如何设置Search组件删除按钮颜色?

A:参考示例设置symbol类型清除按钮

Q:Search组件点击旁边的‘x’号会触发什么回调?

A:会触发onChange()、onWillChange()、onTextSelectionChange等回调。

在 FAQ 中进行搜索
请输入您想要搜索的关键词