新增隨播廣告支援
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本指南適用於有意在 Android IMA 導入作業中加入隨播廣告的發布商。
必要條件
- 已整合 IMA SDK 的 Android 應用程式。如果您還沒有整合 SDK 的應用程式,請參閱 BasicExample。
- 設定為傳回隨播廣告的廣告代碼。
實用入門資訊
如果仍需在應用程式中導入 IMA SDK,請參閱入門指南。
在應用程式中加入隨播廣告
建立 ViewGroup 來顯示隨播廣告
要求使用智慧助理前,請先在版面配置中建立空間。在版面配置 XML 中新增 ViewGroup
元素;這個範例使用 LinearLayout
。在後續步驟中,您會將這個元素的參照傳遞至 AdDisplayContainer
。如要整合至 BasicExample 應用程式,請將此項目新增至
activity_my.xml
下方的 videoPlayerContainer
。
activity_my.xml
<LinearLayout
android:id="@+id/companionAdSlot"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:orientation="vertical"
android:textAlignment="center" />
建立 CompanionAdSlot
下一步是建構 CompanionAdSlot
物件,然後將該物件新增至 ArrayList<CompanionAdSlot>
。AdDisplayContainer
會取得隨播廣告位置清單,因此您可以一次顯示多個隨播廣告。您需要建立 ImaSdkFactory
的執行個體,才能建立 CompanionAdSlot
。
ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();
ViewGroup companionViewGroup = (ViewGroup) findViewById(R.id.companionAdSlot);
CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot();
companionAdSlot.setContainer(companionViewGroup);
companionAdSlot.setSize(300, 250);
ArrayList<CompanionAdSlot> companionAdSlots = new ArrayList<CompanionAdSlot>();
companionAdSlots.add(companionAdSlot);
為要在應用程式中顯示的每個隨播廣告大小建立隨播廣告版位。IMA SDK 會在隨播廣告版位中填入 VAST 回應中的任何隨播廣告,這些廣告的尺寸必須與檢視區塊的高度和寬度相符。IMA SDK 也支援使用自動調整大小的隨播廣告。建立 companionAdSlots
後,必須將其新增至 AdsLoader
。以下範例說明如何執行這項操作,具體做法取決於您是否使用 IMA Android BasicExample (使用 Exoplayer-IMA 擴充功能),或是其他未使用擴充功能的 IMA 實作方式。
BasicExample
adsLoader = new ImaAdsLoader.Builder(this).setCompanionAdSlots(companionAdSlots).build();
其他實作資訊
adsLoader.getAdDisplayContainer().setCompanionSlots(companionAdSlots);
這就是所有訣竅了!應用程式現在會顯示隨播廣告。
顯示自動調整隨播廣告
IMA 現在支援自動調整大小的隨播廣告。這些隨播廣告可調整大小,以符合廣告版位大小。這些範本會填滿上層檢視區塊的寬度,然後調整高度以配合隨播廣告的內容。您可以在 Ad Manager 中使用Fluid
隨播廣告大小設定。請參閱下圖,瞭解如何設定這個值。
更新 Android 應用程式,以便順暢使用隨身裝置
如要宣告隨機大小的隨播廣告空間,請更新 CompanionAdSlot.setSize()
方法,將 CompanionAdSlot.FLUID_SIZE
做為兩個參數。
ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();
ViewGroup companionViewGroup = (ViewGroup) findViewById(R.id.companionAdSlot);
CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot();
companionAdSlot.setContainer(companionViewGroup);
companionAdSlot.setSize(CompanionAdSlot.FLUID_SIZE, CompanionAdSlot.FLUID_SIZE);
ArrayList<CompanionAdSlot> companionAdSlots = new ArrayList<CompanionAdSlot>();
companionAdSlots.add(companionAdSlot);
常見問題
- 我按照指南操作,但沒有看到隨播廣告。該怎麼辦?
- 首先,請檢查代碼是否確實傳回隨播廣告。如要這麼做,請在網路瀏覽器中開啟代碼,然後尋找 CompanionAds 代碼。如果看到該訊息,請檢查傳回的隨播廣告大小是否與您傳遞至
CompanionAdSlot
物件的尺寸相同。 - 按照本指南操作後,隨播廣告版位會是什麼樣子?
-
下圖是根據
BasicExample
建立,內容影片在上方播放,下方則顯示隨播廣告。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThis guide helps publishers integrate companion ads into their Android applications using the IMA SDK.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers need an Android app with the IMA SDK and an ad tag configured to return companion ads to begin.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves creating a ViewGroup in the layout, a CompanionAdSlot object, and adding it to the AdsLoader.\u003c/p\u003e\n"],["\u003cp\u003eThe IMA SDK supports fluid companion ads that resize to fit the ad slot using \u003ccode\u003eCompanionAdSlot.FLUID_SIZE\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTroubleshooting tips are provided for common issues, such as not seeing companion ads after implementation.\u003c/p\u003e\n"]]],[],null,["Select platform: [HTML5](/interactive-media-ads/docs/sdks/html5/client-side/companions \"View this page for the HTML5 platform docs.\") [Android](/interactive-media-ads/docs/sdks/android/client-side/companions \"View this page for the Android platform docs.\") [iOS](/interactive-media-ads/docs/sdks/ios/client-side/companions \"View this page for the iOS platform docs.\")\n\nThis guide is intended for publishers interested in adding companion ads to\ntheir Android IMA implementation.\n\nPrerequisites\n\n- Android application with the IMA SDK integrated. See the [BasicExample](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/) if you don't already have an app with the SDK integrated.\n- An ad tag configured to return a companion ad.\n - If you need a sample, check out our [FAQ](/interactive-media-ads/faq#4).\n\nHelpful primers\n\nIf you still need to implement the IMA SDK in your app, check out our [Get\nStarted guide](/interactive-media-ads/docs/sdks/android/client-side/quickstart).\n\nAdd companion ads to your app\n\nCreate a ViewGroup to display your companion\n\nBefore requesting a companion, you need to create a space for it in your\nlayout. In your layout XML, add a `ViewGroup` element; this\nexample uses a `LinearLayout`. In a later step you'll pass a\nreference to this element to your `AdDisplayContainer`.\n\nIf you're\nintegrating into the BasicExample app, add this to [`activity_my.xml`](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/app/src/main/res/layout/activity_my.xml) below the `videoPlayerContainer`.\n\nactivity_my.xml \n\n```scdoc\n\u003cLinearLayout\n android:id=\"@+id/companionAdSlot\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"250dp\"\n android:layout_gravity=\"center_horizontal\"\n android:gravity=\"center\"\n android:orientation=\"vertical\"\n android:textAlignment=\"center\" /\u003e\n```\n\nCreate a CompanionAdSlot\n\nThe next step is to build a `CompanionAdSlot` object, which is\nthen added to an `ArrayList\u003cCompanionAdSlot\u003e`.\n`AdDisplayContainer` takes a list of companion ad slots so you can\ndisplay multiple companion ads at once. You will need to create an instance of\n`ImaSdkFactory` to create the `CompanionAdSlot`. \n\n```text\n ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();\n ViewGroup companionViewGroup = (ViewGroup) findViewById(R.id.companionAdSlot);\n\n CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot();\n companionAdSlot.setContainer(companionViewGroup);\n companionAdSlot.setSize(300, 250);\n ArrayList\u003cCompanionAdSlot\u003e companionAdSlots = new ArrayList\u003cCompanionAdSlot\u003e();\n companionAdSlots.add(companionAdSlot);\n```\n\nCreate a companion ad slot for each size of companion you intend to show in your app.\nThe IMA SDK populates the companion ad slot with any companions from the VAST response that\nhave dimensions matching the view's height and width. The IMA SDK also supports using\n[fluid sized companions](/interactive-media-ads/docs/sdks/android/client-side/companions#display-fluid-companion-ads).\nOnce you have created the `companionAdSlots` they needed to be added to the\n`AdsLoader`. The following examples show how to do this depending on whether you\nare using the\n[IMA Android BasicExample](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/) which uses the\n[Exoplayer-IMA extension](//github.com/google/ExoPlayer/tree/release-v2/extensions/ima),\nor other IMA implementations that do not use the extension. \n\nBasicExample \n\n```text\nadsLoader = new ImaAdsLoader.Builder(this).setCompanionAdSlots(companionAdSlots).build();\n```\n\nOther implementations \n\n```text\nadsLoader.getAdDisplayContainer().setCompanionSlots(companionAdSlots);\n```\n\nThat's all there is to it! Your application is now displaying companion\nads.\n\nDisplay fluid companion ads\n\nIMA now supports fluid companion ads. These companions ads can resize to match the size of the ad\nslot. They fill 100% of the width of parent view, then resize their height to fit the companion's\ncontent. They're set by using the `Fluid` companion size in Ad Manager. See the\nfollowing image for where to set this value.\n\nUpdate Android apps for fluid companions\n\nYou can declare a fluid companion slot by updating the\n[`CompanionAdSlot.setSize()`](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/CompanionAdSlot#public-abstract-void-setsize-int-width,-int-height)\nmethod to take [`CompanionAdSlot.FLUID_SIZE`](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/CompanionAdSlot#public-static-final-int-fluid_size)\nas both parameters. \n\n```scdoc\n ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();\n ViewGroup companionViewGroup = (ViewGroup) findViewById(R.id.companionAdSlot);\n\n CompanionAdSlot companionAdSlot = sdkFactory.createCompanionAdSlot();\n companionAdSlot.setContainer(companionViewGroup);\n companionAdSlot.setSize(CompanionAdSlot.FLUID_SIZE, CompanionAdSlot.FLUID_SIZE);\n ArrayList\u003cCompanionAdSlot\u003e companionAdSlots = new ArrayList\u003cCompanionAdSlot\u003e();\n companionAdSlots.add(companionAdSlot);\n```\n\nFAQ\n\nI followed the guide, but I'm not seeing companion ads. What should I do?\n: First, check to make sure your tag really is returning companions. To do\n this, open the tag in a web browser and look for a CompanionAds tag. If you see\n that, check to make sure the size of the companion being returned is the same\n size as the dimensions you're passing into the `CompanionAdSlot`\n object.\n\nWhat will my companion ad slot look like when following this guide?\n:\n The image below was created from the [BasicExample](https://github.com/googleads/googleads-ima-android/blob/main/basicexample/) and has the content video playing above with the companion ad below.\n\n \u003cbr /\u003e"]]