1e41f4b71Sopenharmony_ci# @ohos.advertising.AdComponent (非全屏广告展示组件) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci本模块提供展示非全屏广告的能力。 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci> **说明:** 8e41f4b71Sopenharmony_ci> 本模块首批接口从API Version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 导入模块 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { AdComponent } from '@kit.AdsKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## AdComponent 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciAdComponent(ads: Array<advertising.Advertisement>, displayOptions: advertising.AdDisplayOptions, interactionListener: advertising.AdInteractionListener, adRenderer?:() => void): void 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci展示非全屏广告。 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Advertising.Ads 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**参数:** 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| **参数名** | **类型** | 必填 | 说明 | 30e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 31e41f4b71Sopenharmony_ci| ads | Array<advertising.[Advertisement](js-apis-advertising.md#advertisement)> | 是 | 广告对象数组。<br/>原子化服务API:从API version 12开始,该接口支持在原子化服务中使用。 | 32e41f4b71Sopenharmony_ci| displayOptions | advertising.[AdDisplayOptions](js-apis-advertising.md#addisplayoptions) | 是 | 广告展示参数。<br/>原子化服务API:从API version 12开始,该接口支持在原子化服务中使用。 | 33e41f4b71Sopenharmony_ci| interactionListener | advertising.[AdInteractionListener](js-apis-advertising.md#adinteractionlistener) | 是 | 广告状态变化回调。<br/>原子化服务API:从API version 12开始,该接口支持在原子化服务中使用。 | 34e41f4b71Sopenharmony_ci| adRenderer<sup>12+</sup> | () => void | 否 | 应用自渲染广告样式。 | 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**示例:** 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci```ts 39e41f4b71Sopenharmony_ciimport { AdComponent, advertising } from '@kit.AdsKit'; 40e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci@Entry 43e41f4b71Sopenharmony_ci@Component 44e41f4b71Sopenharmony_ciexport struct ShowNonFullScreenAd { 45e41f4b71Sopenharmony_ci // 请求到的广告内容 46e41f4b71Sopenharmony_ci private ads: Array<advertising.Advertisement> = []; 47e41f4b71Sopenharmony_ci // 广告展示参数 48e41f4b71Sopenharmony_ci private adDisplayOptions: advertising.AdDisplayOptions = { 49e41f4b71Sopenharmony_ci // 是否静音,默认不静音 50e41f4b71Sopenharmony_ci mute: false 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci build() { 54e41f4b71Sopenharmony_ci Column() { 55e41f4b71Sopenharmony_ci // AdComponent组件用于展示非全屏广告 56e41f4b71Sopenharmony_ci AdComponent({ 57e41f4b71Sopenharmony_ci ads: this.ads, displayOptions: this.adDisplayOptions, 58e41f4b71Sopenharmony_ci interactionListener: { 59e41f4b71Sopenharmony_ci // 广告状态变化回调 60e41f4b71Sopenharmony_ci onStatusChanged: (status: string, ad: advertising.Advertisement, data: string) => { 61e41f4b71Sopenharmony_ci switch (status) { 62e41f4b71Sopenharmony_ci case 'onAdOpen': 63e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onAdOpen'); 64e41f4b71Sopenharmony_ci break; 65e41f4b71Sopenharmony_ci case 'onAdClick': 66e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onAdClick'); 67e41f4b71Sopenharmony_ci break; 68e41f4b71Sopenharmony_ci case 'onAdClose': 69e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onAdClose'); 70e41f4b71Sopenharmony_ci break; 71e41f4b71Sopenharmony_ci } 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci } 74e41f4b71Sopenharmony_ci }) 75e41f4b71Sopenharmony_ci .width('100%') 76e41f4b71Sopenharmony_ci .height('100%') 77e41f4b71Sopenharmony_ci }.width('100%').height('100%') 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci} 80e41f4b71Sopenharmony_ci```