【每日學點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()方法將滑動效果進行恢復
參考文檔如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5#closeallswipeactions11
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收起,并設置回調事件。