【每日学点HarmnoyOS Next知识】web加载pdf、Toggle禁用、Grid多次渲染问题、Web判断是否存在title、 List侧滑栏关闭
1、HarmonyOS Web组件加载本地pdf文件后,默认显示标题和下载按钮,可以隐藏或者有对应的操作这个title的API吗?
隐藏PDF操作按钮栏,后面加#toolbar=0&navpanes=0即可,举例: src:"file://" + filesDir + "/test.pdf#toolbar=0&navpanes=0"
2、HarmonyOS Toggle如何在特定场景下禁用,即点击依旧保持false的状态,不会改变?
在多个Toggle来设置状态的情况下,如果第一个Toggle是false,则禁用第二个和第三个Toggle,即都设置成false,且点击这两个Toggle无法将false变为true
参考demo如下:
@Entry
@Component
struct ToggleExample {
@State is_on:boolean = false;
build() {
Column() {
Row() {
Text("Bluetooth Mode")
.height(50)
.fontSize(16)
}
Row() {
Text("Bluetooth")
.height(50)
.padding({left: 10})
.fontSize(16)
.textAlign(TextAlign.Start)
.backgroundColor(0xFFFFFF)
Stack(){
Toggle({ type: ToggleType.Switch ,isOn:this.is_on})
.margin({left: 200, right: 10})
Column(){
}.width(60)
.height(50)
.margin({left: 200, right: 10})
.onClick(()=>{
this.is_on = !this.is_on
})
}
}
.backgroundColor(0xFFFFFF)
}
.padding(10)
.backgroundColor(0xDCDCDC)
.width('100%')
.height('100%')
}
}
3、HarmonyOS Grid多次渲染会固定布局 如历史搜索记录展示,搜索文字长度不固定?
可以参考使用拉伸能力实现效果,拉伸能力是指容器组件尺寸发生变化时,增加或减小的空间全部分配给容器组件内指定区域,通常通过Flex布局中的flexGrow和flexShrink属性实现,具体使用可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/adaptive-layout-V5
可以加上FlexWrap.Wrap这个属性来换行,参考demo
@Entry
@Component
struct ListExample {
private arr: string[] = ['测试1111', '测试', '测试0', '测试112222', '测试112222yeyeyeyy', '测试1', '测试112222yeyeyeyyjdjkd']
build() {
Column() {
Row() {
Flex({ wrap: FlexWrap.Wrap }) {
ForEach(this.arr, (item: string) => {
Text(item)
.height(50)
.fontSize(16)
.textAlign(TextAlign.Center)
.borderRadius(10)
.backgroundColor(0xFFFFFF)
.width('30%')
.flexGrow(1)
.flexShrink(0)
},
(item: string) => item)
}
}
.height('100%')
.width('100%')
.backgroundColor(Color.Pink)
.justifyContent(FlexAlign.SpaceEvenly)
}
.width('100%').height('100%')
.backgroundColor(0xDCDCDC).padding({ top: 5 })
}
}
4、HarmonyOS Web如何判断元素是否存在Title?
目前看到Web的document中没有title标签,在onTitleReceive和controller.getTitle默认返回了不带协议头的url链接,没找到文档有描述,是否可以判断Title标签是否存在?是否可以自己对比去掉协议头的url字符串进行判断?
web中无检测Title的接口,可以在前端或使用JS注入的方式检测是否存在Title。
<script>
if(document.title){
alert("该页面存在title,标题为:" + document.title);
}else{
alert("该页面不存在title");
}
</script>
5、HarmonyOS ListItem的侧滑栏如何主动关闭?
可以使用ListScroller提供的closeAllSwipeActions()方法将滑动效果进行恢复
closeAllSwipeActions(options?: [CloseSwipeActionOptions](https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5#closeswipeactionoptions11%E5%AF%B9%E8%B1%A1%E8%AF%B4%E6%98%8E)): void
将EXPANDED状态的ListItem收起,并设置回调事件。