首页设置壁纸及修改部分WebView

This commit is contained in:
DevWiki 2024-04-11 14:04:42 +08:00
parent aba6717228
commit fe18fa1ca6
5 changed files with 30 additions and 12 deletions

View File

@ -54,7 +54,7 @@ struct Index {
.width('100%') .width('100%')
.alignRules({ .alignRules({
top: { anchor: ComponentConst.ContainerId, align: VerticalAlign.Top }, top: { anchor: ComponentConst.ContainerId, align: VerticalAlign.Top },
left: { anchor: ComponentConst.ContainerId, align: HorizontalAlign.Start } left: { anchor: ComponentConst.ContainerId, align: HorizontalAlign.Start },
}).id("title_bar"); //必须要设置id 否则不显示 }).id("title_bar"); //必须要设置id 否则不显示
Divider().alignRules({ Divider().alignRules({
@ -84,7 +84,7 @@ struct Index {
Button("1").type(ButtonType.Capsule).height(32).width(80); Button("1").type(ButtonType.Capsule).height(32).width(80);
}.rowStart(1).rowEnd(1).columnStart(0).columnEnd(0) }.rowStart(1).rowEnd(1).columnStart(0).columnEnd(0)
} }
.width('100%').backgroundColor('#FF005566') .width('100%')
.columnsTemplate('1fr 1fr 1fr 1fr').columnsGap(8) .columnsTemplate('1fr 1fr 1fr 1fr').columnsGap(8)
.rowsTemplate('repeat(auto-fill, 36)').rowsGap(8) .rowsTemplate('repeat(auto-fill, 36)').rowsGap(8)
.alignRules({ .alignRules({
@ -93,6 +93,6 @@ struct Index {
bottom: { anchor: ComponentConst.ContainerId, align: VerticalAlign.Bottom } bottom: { anchor: ComponentConst.ContainerId, align: VerticalAlign.Bottom }
}).id("host_web") //必须要设置id 否则不显示 }).id("host_web") //必须要设置id 否则不显示
}.width('100%').height('100%') }.width('100%').height('100%')
}.width('100%').height('100%') }.width('100%').height('100%').backgroundImage($r("app.media.bg_sky")).backgroundImageSize(ImageSize.Cover)
} }
} }

View File

@ -1,4 +1,5 @@
import { WebView, ComponentConst, CommonRes, TitleBar, WebViewController, ScreenUtil } from '@devwiki/common_ui'; import { WebView, ComponentConst, CommonRes, TitleBar, WebViewController, ScreenUtil } from '@devwiki/common_ui';
import { WebViewParam } from '@devwiki/common_ui/src/main/ets/component/web/WebView';
@Entry @Entry
@Component @Component
@ -40,7 +41,7 @@ export struct WebPage {
left: { anchor: ComponentConst.ContainerId, align: HorizontalAlign.Start } left: { anchor: ComponentConst.ContainerId, align: HorizontalAlign.Start }
}).width('100%').id("divider") }).width('100%').id("divider")
WebView({ webUrl: this.isPortrait? this.viewModel?.webUrl : "https://blog.devwiki.net", controller: this.webViewController }).width('100%') WebView({ param: this.viewModel.webParam, controller: this.webViewController }).width('100%')
.alignRules({ .alignRules({
top: { anchor: "divider", align: VerticalAlign.Bottom }, top: { anchor: "divider", align: VerticalAlign.Bottom },
left: { anchor: ComponentConst.ContainerId, align: HorizontalAlign.Start }, left: { anchor: ComponentConst.ContainerId, align: HorizontalAlign.Start },
@ -70,7 +71,11 @@ class WebPageController extends WebViewController {
} }
class WebPageViewModel { class WebPageViewModel {
webUrl: string = "https://devwiki.net";
webParam: WebViewParam = {
webUrl: "https://devwiki.net"
};
pageTitle: ResourceStr = ""; pageTitle: ResourceStr = "";
constructor() { constructor() {

Binary file not shown.

After

Width:  |  Height:  |  Size: 585 KiB

View File

@ -85,8 +85,8 @@ export struct TitleBar {
align: this.leftMenuType != TitleBarMenuType.None ? HorizontalAlign.End : HorizontalAlign.Start align: this.leftMenuType != TitleBarMenuType.None ? HorizontalAlign.End : HorizontalAlign.Start
}, },
right: { right: {
anchor: this.leftMenuType != TitleBarMenuType.None ? "right_menu" : ComponentConst.ContainerId, anchor: this.rightMenuType != TitleBarMenuType.None ? "right_menu" : ComponentConst.ContainerId,
align: this.leftMenuType != TitleBarMenuType.None ? HorizontalAlign.Start : HorizontalAlign.End align: this.rightMenuType != TitleBarMenuType.None ? HorizontalAlign.Start : HorizontalAlign.End
}, },
bottom: { anchor: ComponentConst.ContainerId, align: VerticalAlign.Bottom } bottom: { anchor: ComponentConst.ContainerId, align: VerticalAlign.Bottom }
}) })

View File

@ -1,24 +1,34 @@
import web_webview from '@ohos.web.webview'; import web_webview from '@ohos.web.webview';
import { WebScriptCallback, WebScriptFunction } from './WebScript'; import { WebScriptCallback, WebScriptFunction } from './WebScript';
export class WebViewParam {
webUrl: string = "";
}
@Component @Component
export struct WebView { export struct WebView {
private webviewController: web_webview.WebviewController = new web_webview.WebviewController(); private webviewController: web_webview.WebviewController = new web_webview.WebviewController();
controller: WebViewController = new WebViewController(); controller: WebViewController = new WebViewController();
@Prop webUrl: string = ""; @Prop @Watch("onParamChanged")param: WebViewParam = new WebViewParam();
onParamChanged() {
if (this.param.webUrl != this.webviewController.getUrl()) {
this.controller.loadUrl(this.param.webUrl);
}
}
aboutToAppear(): void { aboutToAppear(): void {
this.controller.setWebviewController(this.webviewController); this.controller.setWebviewController(this.webviewController);
} }
build() { build() {
Web({ src: this.webUrl, controller: this.webviewController }) Web({ src: this.param.webUrl, controller: this.webviewController })
.javaScriptAccess(true)// 允许使用 js .javaScriptAccess(true)// 允许使用 js
.javaScriptProxy({ .javaScriptProxy({
object: null, object: this.controller,
name: "", name: WebViewController.JsBridgeName,
methodList: [], methodList: WebViewController.JsMethodName,
controller: this.webviewController controller: this.webviewController
}) })
.onPageEnd(() => { this.controller.onPageEnd(this.webviewController.getTitle()) }) .onPageEnd(() => { this.controller.onPageEnd(this.webviewController.getTitle()) })
@ -34,6 +44,9 @@ interface WebViewCallback {
export class WebViewController implements WebScriptCallback, WebScriptFunction, WebViewCallback { export class WebViewController implements WebScriptCallback, WebScriptFunction, WebViewCallback {
static readonly JsBridgeName = "devwiki";
static readonly JsMethodName = [""];
private webviewController?: web_webview.WebviewController private webviewController?: web_webview.WebviewController
constructor() { constructor() {