AdManagerAdViewadView=newAdManagerAdView(this);adView.setAdSizes(AdSize.BANNER);adView.setAdUnitId("ca-mb-app-pub-5629679302779023/");// TODO: Add adView to your view hierarchy.
Kotlin
valadView=AdManagerAdView(this)adView.adSizes=AdSize.BANNERadView.adUnitId="ca-mb-app-pub-5629679302779023/"// TODO: Add adView to your view hierarchy.
请注意,如果未能向 Ad Exchange 网站资源代码添加尾随正斜杠,会导致广告请求错误,并显示以下消息:
Invalid Request. Cannot determine request type. Is your ad unit id correct?
您还可以将 Ad Exchange 网站媒体资源代码转换为广告单元。
然后,使用 Ad Manager 界面生成 Ad Exchange 代码,并将其复制到您的应用中。生成的代码应包含 Ad Exchange Web 资源代码,后跟子级广告单元 ID(不含尾部正斜杠),例如:ca-mb-app-pub-5629679302779023/banner。
大功告成!您的应用现已准备好加载和展示 Ad Exchange 横幅广告了。
此外,您还可以按照以下相应指南,使用 Ad Exchange 网站媒体资源加载和展示 Ad Exchange 中的其他广告格式:
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis guide explains how to configure your Android app to display ads from your Ad Exchange property using the Google Mobile Ads SDK.\u003c/p\u003e\n"],["\u003cp\u003eYou need to add your Ad Manager app ID to your AndroidManifest.xml file and use your Ad Exchange web property code as the ad unit ID, ensuring it has a trailing forward slash.\u003c/p\u003e\n"],["\u003cp\u003eThe guide primarily focuses on banner ads but the same principles apply to other ad formats like interstitial, native, and rewarded ads.\u003c/p\u003e\n"],["\u003cp\u003eApproved European publishers can request the "Price floors" feature and include public or private floor prices in their ad requests using specific parameters.\u003c/p\u003e\n"]]],[],null,["Select platform: [Android](/ad-manager/mobile-ads-sdk/android/adx-direct \"View this page for the Android platform docs.\") [iOS](/ad-manager/mobile-ads-sdk/ios/adx-direct \"View this page for the iOS platform docs.\")\n\n\u003cbr /\u003e\n\nThe Google Mobile Ads SDK supports monetizing your apps with an [Ad Exchange\nproperty](//support.google.com/admanager/answer/6321605). This guide shows you\nhow to configure your apps for all ad formats supported by Ad Exchange.\n\nPrerequisites\n\n- [Complete the\n prerequisites](/ad-manager/mobile-ads-sdk/android/quick-start#app_prerequisites).\n- [Configure your\n app](/ad-manager/mobile-ads-sdk/android/quick-start#import_the_mobile_ads_sdk) for the Google Mobile Ads SDK.\n\nConfigure your app for accessing Ad Exchange\n\nAdd your Ad Manager app ID ([identified in the Ad Manager\nUI](//support.google.com/admanager/answer/1656921)) to your app's\nAndroidManifest.xml file through a `\u003cmeta-data\u003e` tag with\n`android:name=\"com.google.android.gms.ads.APPLICATION_ID\"`. For `android:value`,\ninsert your own Ad Manager app ID, surrounded by quotation marks. \n\n \u003cmanifest\u003e\n \u003capplication\u003e\n \u003c!-- Sample Ad Manager app ID: ca-app-pub-3940256099942544~3347511713 --\u003e\n \u003cmeta-data\n android:name=\"com.google.android.gms.ads.APPLICATION_ID\"\n android:value=\"ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy\"/\u003e\n \u003c/application\u003e\n \u003c/manifest\u003e\n\nNote also that failure to add the `\u003cmeta-data\u003e` tag as shown above results in a\ncrash with the message: \n\n Missing application ID.\n\nNext, you can [initialize the Google Mobile Ads\nSDK](/ad-manager/mobile-ads-sdk/android/quick-start#initialize_the_mobile_ads_sdk)\nand select an ad format to display. The rest of this guide implements the\n[banner format](/ad-manager/mobile-ads-sdk/android/banner) to\nillustrate how you can load an ad from Ad Exchange. The same steps can apply to\nany ad formats supported by the Google Mobile Ads SDK.\n\n\nLoad an ad from Ad Exchange\n\nYou can use an Ad Exchange web property code with a trailing forward slash, in\nplace of an ad unit ID. For example, `ca-mb-app-pub-5629679302779023/`.\n\nFor example, to load a banner ad by placing an `AdManagerAdView` in the layout\nfor the `Activity` as follow: \n\n # main_activity.xml\n ...\n \u003ccom.google.android.gms.ads.admanager.AdManagerAdView\n xmlns:ads=\"http://schemas.android.com/apk/res-auto\"\n android:id=\"@+id/adManagerAdView\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:layout_centerHorizontal=\"true\"\n android:layout_alignParentBottom=\"true\"\n ads:adSize=\"BANNER\"\n ads:adUnitId=\"ca-mb-app-pub-5629679302779023/\"\u003e\n \u003c/com.google.android.gms.ads.admanager.AdManagerAdView\u003e\n\n| **Key Point:** Make sure you set the ad size and ad unit ID in the same manner, that is, set both in XML or both programmatically.\n\n\nAlternatively, you can create a view hierarchy programmatically: \n\nJava \n\n AdManagerAdView adView = new AdManagerAdView(this);\n\n adView.setAdSizes(AdSize.BANNER);\n adView.setAdUnitId(\"ca-mb-app-pub-5629679302779023/\");\n\n // TODO: Add adView to your view hierarchy.\n\nKotlin \n\n val adView = AdManagerAdView(this)\n\n adView.adSizes = AdSize.BANNER\n adView.adUnitId = \"ca-mb-app-pub-5629679302779023/\"\n\n // TODO: Add adView to your view hierarchy.\n\nOnce the `AdManagerAdView` is in place, you can [call the `loadAd()`\nmethod](/ad-manager/mobile-ads-sdk/android/banner#load_an_ad) in the\n`AdManagerAdView` class and customize the behavior of your ad using [ad\nevents](/ad-manager/mobile-ads-sdk/android/banner#ad_events).\n\nNote that failure to add a trailing forward slash to the Ad Exchange web\nproperty code results in an ad request error with the message: \n\n Invalid Request. Cannot determine request type. Is your ad unit id correct?\n\nYou can also convert an Ad Exchange web property code into an ad unit.\nAfterwards, use the Ad Manager UI to [generate an Ad Exchange\nTag](//support.google.com/admanager/answer/7501422) and copy it into your\napp. The generated tag should have the Ad Exchange web property code, followed\nby descendant ad unit IDs without a trailing forward slash, for example:\n`ca-mb-app-pub-5629679302779023/banner`.\n\nThat's it! Your app is now ready to load and display banner ads from Ad Exchange.\n\nIn addition, you can use an Ad Exchange web property to load and display other\nad formats from Ad Exchange by following respective guides:\n\n- [Interstitial](/ad-manager/mobile-ads-sdk/android/quick-start#interstitial)\n- [Native](/ad-manager/mobile-ads-sdk/android/quick-start#native)\n- [Rewarded](/ad-manager/mobile-ads-sdk/android/quick-start#rewarded)\n\n(Approved European publishers only) Add price floors\n\nYou can [submit a request for the \"Price floors\"\nfeature](//support.google.com/admanager/answer/7501422#submit-request).\n\nOnce approved, you can include a public floor or private floor in the ad request\nusing the `pubf` and `pvtf` parameters respectively. In the following code\nexample, replace \"123\" with the floor prices in micros and your [network's\ndefault currency](//support.google.com/admanager/answer/177495). Example of how\nmicros are applied: if your default currency is USD, entering \"6000000\" is the\nequivalent of $6.00. \n\nJava \n\n Bundle extras = new Bundle();\n // Public floor parameter.\n extras.putString(\"pubf\", \"123\");\n // Private floor parameter.\n extras.putString(\"pvtf\", \"123\");\n\n AdManagerAdRequest request = new AdManagerAdRequest.Builder()\n .addNetworkExtrasBundle(AdMobAdapter.class, extras)\n .build();\n\nKotlin \n\n val extras = Bundle();\n // Public floor parameter.\n extras.putString(\"pubf\", \"123\");\n // Private floor parameter.\n extras.putString(\"pvtf\", \"123\");\n\n val request = AdManagerAdRequest.Builder()\n .addNetworkExtrasBundle(AdMobAdapter::class::java, extras)\n .build();"]]