import web_webview from '@ohos.web.webview'; // 子组件 @Component export struct RichTextItem { private src: string = ''; controller: web_webview.WebviewController = new web_webview.WebviewController(); build() { Column() { Web({ src: '', controller: this.controller }) .width('100%') .javaScriptAccess(true) .domStorageAccess(true) .geolocationAccess(false) .mixedMode(MixedMode.None) .fileAccess(true) .imageAccess(true) .cacheMode(CacheMode.None) .onlineImageAccess(true) .layoutMode(WebLayoutMode.FIT_CONTENT) // 使得web高度自适应 .onControllerAttached(() => { this.controller.loadData(this.src, 'text/html', 'utf-8', ' ', ' '); }) .onSslErrorEvent(event => { event.handler.handleConfirm(); }) } } } @Entry @Component struct ListWebViewSolutionOne { @State richTextList: Array<string> = [ ` <span style="font-size:12.5px;"><span style="color:#666666;line-height:1.5;"><span style="font-size:12.5px;"><span style="color:#666666;"></span></span></span><span style="color:#666666;line-height:1.5;">【超级0卡糖*·低负担·0咖啡】</span><br /> <br /> <span style="color:#0A59F7;line-height:1.5;">不含咖啡的友好小铁</span><br /> <span style="color:#0A59F7;line-height:1.5;">陪伴午后的闲暇时光</span><br /> <br /> <span style="color:#999;line-height:1.5;">「超级0卡糖*」原创定制</span><br /> <span style="color:#666666;line-height:1.5;">果味清爽沁甜</span><br /> <br /> <span style="color:#444;line-height:1.5;">感受青提风味在奶香和椰香中游走</span><br /> <span style="color:#555;line-height:1.5;">轻盈地,过夏天</span><br /> <br /> <span style="color:#777;line-height:1.5;">*本品使用0卡青提风味饮料浓浆(含赤藓糖醇)</span><br /> </span> `, ` <div> <p>我是 P1</p> <span><font color="#0A59F7">我是span1</font></span> <span>我是span2</span> <p>我是 P2</p> <span><font color="#0A59F7">我是span3</font></span> <span>我是span4</span> </div> `, ` <div><font color="gray">兑换说明</font><br>赠送15.0%;<br></div> <div><font color="#0A59F7">兑换说明</font><br><font color="blue">赠送30.0%;</font><br></div> <font color="gray">兑换说明</font><br><font color="blue">赠送60.0%;</font><br> <font color="#0A59F7">兑换说明</font><font color="blue">赠送125.0%;</font><br> `, ` <span style=\"color:#999999;\">手机的实名人需与我的信息为</span> <span style=\"color:#0A59F7;\">同一人</span> <span style=\"color:#999999;\">,否则请更换账户或手机号码</span> `, ` <span style='text-align:center;'>您好, 现在会员促销期间<font color='#0A59F7'>全场商品打6折</font>哟~错过时间会<font color='#0A59F7'>恢复原价</font>哈!</span> `, `<font color='#0A59F7'>宝宝</font><font color='#0A59F7'>不爱吃饭</font>别怕!有它轻松拿捏挑食<font color='#0A59F7'>宝宝</font>!我家<font color='#0A59F7'>宝宝</font>不知是随...#<font color='#0A59F7'>宝宝</font>营养补充#`, ` <span style='text-align:center;'>您好, 现在会员促销期间<font color='#0A59F7'>打6折</font>哟~错过时间<font color='#0A59F7'>恢复原价</font>哈!</span> `, '<p>欢迎来到松山湖华为溪流背坡村</p><table><tbody><tr class="firstRow"><td width="204" valign="top"><img src="https://www-file.huawei.com/-/media/corporate/images/press%20center/facilities%20around%20the%20world/2019/xcun-0404.jpg?w=500" style="width:100%;"/></td><td width="204" ' + 'valign="top"><img src="https://www-file.huawei.com/-/media/corporate/images/press%20center/facilities%20around%20the%20world/2019/xcun-0406.jpg?w=500" style="width:100%;"/></td></tr><tr><td width="204" valign="top"><br/></td><td width="204" valign="top"><br/></td></tr></tbody></table><p><br/></p>', '<p><img src="https://www-file.huawei.com/-/media/corporate/images/press%20center/facilities%20around%20the%20world/2017/headquarter-dr-center.jpg?w=1000"/></p>', ]; controller: web_webview.WebviewController = new web_webview.WebviewController(); build() { Column() { List({ space: 8 }) { ForEach(this.richTextList, (item: string) => { ListItem() { RichTextItem({ src: this.getHtmlText(item) }) } .borderRadius(8) .backgroundColor(Color.White) .padding({ top: 8, right: 12, bottom: 8, left: 12 }) }, (item: string) => item) } .scrollBar(BarState.Off) } .backgroundColor(Color.Gray) .padding(8) } // 使用完整的html片段加载 getHtmlText(src: string) { let msg = ` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"/> </head> <body> ${src} </body> </html>`; return msg; } }
import web_webview from '@ohos.web.webview'; @Entry @Component struct ListWebViewSolutionTwo { @State richTextList: Array<string> = [ ` <span style="font-size:12.5px;"><span style="color:#666666;line-height:1.5;"><span style="font-size:12.5px;"><span style="color:#666666;"></span></span></span><span style="color:#666666;line-height:1.5;">【超级0卡糖*·低负担·0咖啡】</span><br /> <br /> <span style="color:#0A59F7;line-height:1.5;">不含咖啡的友好小铁</span><br /> <span style="color:#0A59F7;line-height:1.5;">陪伴午后的闲暇时光</span><br /> <br /> <span style="color:#999;line-height:1.5;">「超级0卡糖*」原创定制</span><br /> <span style="color:#666666;line-height:1.5;">果味清爽沁甜</span><br /> <br /> <span style="color:#444;line-height:1.5;">感受青提风味在奶香和椰香中游走</span><br /> <span style="color:#555;line-height:1.5;">轻盈地,过夏天</span><br /> <br /> <span style="color:#777;line-height:1.5;">*本品使用0卡青提风味饮料浓浆(含赤藓糖醇)</span><br /> </span> `, ` <div> <p>我是 P1</p> <span><font color="#0A59F7">我是span1</font></span> <span>我是span2</span> <p>我是 P2</p> <span><font color="#0A59F7">我是span3</font></span> <span>我是span4</span> </div> `, ` <div><font color="gray">兑换说明</font><br>赠送15.0%;<br></div> <div><font color="#0A59F7">兑换说明</font><br><font color="blue">赠送30.0%;</font><br></div> <font color="gray">兑换说明</font><br><font color="blue">赠送60.0%;</font><br> <font color="#0A59F7">兑换说明</font><font color="blue">赠送125.0%;</font><br> `, ` <span style=\"color:#999999;\">手机的实名人需与我的信息为</span> <span style=\"color:#0A59F7;\">同一人</span> <span style=\"color:#999999;\">,否则请更换账户或手机号码</span> `, ` <span style='text-align:center;'>您好, 现在会员促销期间<font color='#0A59F7'>全场商品打6折</font>哟~错过时间会<font color='#0A59F7'>恢复原价</font>哈!</span> `, `<font color='#0A59F7'>宝宝</font><font color='#0A59F7'>不爱吃饭</font>别怕!有它轻松拿捏挑食<font color='#0A59F7'>宝宝</font>!我家<font color='#0A59F7'>宝宝</font>不知是随...#<font color='#0A59F7'>宝宝</font>营养补充#`, ` <span style='text-align:center;'>您好, 现在会员促销期间<font color='#0A59F7'>打6折</font>哟~错过时间<font color='#0A59F7'>恢复原价</font>哈!</span> `, '<p>欢迎来到松山湖华为溪流背坡村</p><table><tbody><tr class="firstRow"><td width="204" valign="top"><img src="https://www-file.huawei.com/-/media/corporate/images/press%20center/facilities%20around%20the%20world/2019/xcun-0404.jpg?w=500" style="width:100%;"/></td><td width="204" ' + 'valign="top"><img src="https://www-file.huawei.com/-/media/corporate/images/press%20center/facilities%20around%20the%20world/2019/xcun-0406.jpg?w=500" style="width:100%;"/></td></tr><tr><td width="204" valign="top"><br/></td><td width="204" valign="top"><br/></td></tr></tbody></table><p><br/></p>', '<p><img src="https://www-file.huawei.com/-/media/corporate/images/press%20center/facilities%20around%20the%20world/2017/headquarter-dr-center.jpg?w=1000"/></p>', ]; controller: web_webview.WebviewController = new web_webview.WebviewController(); build() { Column() { List({ space: 8 }) { ForEach(this.richTextList, (item: string) => { ListItem() { Web({ src: '', controller: this.controller }) .width('100%') .javaScriptAccess(true) .domStorageAccess(true) .mixedMode(MixedMode.None) .geolocationAccess(false) .fileAccess(true) .imageAccess(true) .cacheMode(CacheMode.None) .onlineImageAccess(true) .layoutMode(WebLayoutMode.FIT_CONTENT) // 使得web高度自适应 .onControllerAttached(() => { this.controller.loadData(this.getHtmlText(item), 'text/html', 'utf-8', ' ', ' '); }) .onSslErrorEvent(event => { event.handler.handleConfirm(); }) } .borderRadius(8) .backgroundColor(Color.White) .padding({ top: 8, right: 12, bottom: 8, left: 12 }) }, (item: string) => item) } .scrollBar(BarState.Off) } .backgroundColor(Color.Gray) .padding(8) } // 使用完整的html片段加载 getHtmlText(src: string) { let msg = ` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"/> </head> <body> ${src} </body> </html>`; return msg; } }
执行以上代码需要获取使用Internet网络权限:ohos.permission.INTERNET。
当需要在长列表中用Web组件显示富文本内容时,可以通过两种方法来实现: