diff --git a/ArkUI/entry/src/main/ets/pages/ComponentAttributeCompatibilityJudgment.ets b/ArkUI/entry/src/main/ets/pages/ComponentAttributeCompatibilityJudgment.ets new file mode 100644 index 0000000000000000000000000000000000000000..1f80c1979c8f38ecb42e812833832f8f29d9ae55 --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/ComponentAttributeCompatibilityJudgment.ets @@ -0,0 +1,49 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:如何针对UI组件属性做API版本兼容性判断 +*/ + +// [Start component_attribute_compatibility_judgment] +import { deviceInfo } from '@kit.BasicServicesKit'; + +@Entry +@Component +struct ComponentAttributeCompatibilityJudgment { + modifier: MyListModifier = new MyListModifier(); + + build() { + List() { + // 列表内容 + } + .height('100%') + .width('100%') + .attributeModifier(this.modifier) + } +} + +class MyListModifier implements AttributeModifier { + applyNormalAttribute(instance: ListAttribute): void { + // 通过deviceInfo的api版本信息进行判断 + if (deviceInfo.sdkApiVersion > 14) { + // 要适配的是List组件的backToTop属性 + // instance为List的属性对象,可以通过instance对象对属性进行修改 + instance.backToTop(true); + } + } +} + +// [End component_attribute_compatibility_judgment]