HarmonyOS auto失效

List({ space: 10 }) {
  ForEach(this.bookDetailsBean?.firstSubject?.split("\\/"), (item: string, index) => {
    ListItem() {
      Text(item)
        .fontSize(14)
        .fontColor("#8E8E93")
        .fontWeight(FontWeight.Bold)
        .backgroundColor("#f1f1f1")
        .borderRadius(14)
        .width('auto')
        .padding({
          top: 2,
          bottom: 2,
          left: 4,
          right: 4
        })
    }
  }, (index: number) => index + "")
}.id('_firstSubject').listDirection(Axis.Horizontal).alignRules({
  left: { anchor: 'tv_book_name', align: HorizontalAlign.Start },
  top: { anchor: 'tv_book_name', align: VerticalAlign.Bottom }
}).margin({ top: 15 })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

发现只要这个list不设置固定的高度,list高度不会自适应Listitem的高度Harmonyos的’auto’这个属性是不是没有用

HarmonyOS
2024-12-18 16:46:14
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

若想要父组件跟随子组件的宽高进行自适应,需要在父组件上设置宽高为“auto”当前list横向滚动是无法自适应子组件高度

可参考如下demo:

@Entry
@Component
struct Index {
  build() {
    Column() {
      // 外层套Scroll组件可以使横向滚动的list高度自适应,scroll高度无穷大,list组件不能继承scroll的高度无穷大,所以会根据子组件高度自适应 //
      Scroll() {
        List() {
          ForEach([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], (index: number) => {
            ListItem() {
              Text(`tab_${index}`).height(300)
                .padding({
                  left: 10,
                  top: 8,
                  right: 10,
                  bottom: 8
                })
            }
          })
        }
        .listDirection(Axis.Horizontal)
        .backgroundColor('#ff0000')
        .margin({ top: 60 })
      }

    }.width('100%')
    .height('100%').backgroundColor('#FF15B3B3')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
分享
微博
QQ
微信
回复
2024-12-18 18:44:13
相关问题
RelativeContainer组件auto对齐规则
843浏览 • 1回复 待解决
HarmonyOS 设置userAgent失效
653浏览 • 1回复 待解决
HarmonyOS grid组件的auto-fit没效果
943浏览 • 1回复 待解决
HarmonyOS webView缓存失效
475浏览 • 1回复 待解决
HarmonyOS notifyDataAdd失效问题
851浏览 • 0回复 待解决
HarmonyOS instanceof判断失效
642浏览 • 1回复 待解决
HarmonyOS后台播放失效问题
1133浏览 • 1回复 待解决
HarmonyOS keyboardHeightChange键盘监听失效
924浏览 • 1回复 待解决
HarmonyOS 网络变化监听失效
786浏览 • 1回复 待解决
HarmonyOS 屏幕常亮设置失效
688浏览 • 1回复 待解决
HarmonyOS 多语言修改失效
975浏览 • 1回复 待解决
HarmonyOS 安全区域失效
795浏览 • 1回复 待解决
HarmonyOS WebView 圆角设置失效
815浏览 • 1回复 待解决
HarmonyOS 卡片点击事件失效
903浏览 • 1回复 待解决
HarmonyOS Web 图片上传功能失效
868浏览 • 1回复 待解决
HarmonyOS git库屏蔽文件失效
618浏览 • 1回复 待解决
HarmonyOS Span的属性设置失效
555浏览 • 1回复 待解决
HarmonyOS Image获取焦点和失去焦点失效
614浏览 • 1回复 待解决