缺少 NativeExpressAdView XML 属性 adSize
NativeExpressAdView XML attribute adSize was missing
我正在尝试使用 NativeExpressAdView 但它显示的不是广告
Required XML attribute 'adSize' was missing
黑色背景,红色边框框。我像这样以编程方式将 adUnit 和 adSize 添加到 adView
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/nativeAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
Java代码:
nativeAd = (NativeExpressAdView) itemView.findViewById(R.id.nativeAdView);
int widthInDP = context.getResources().getConfiguration().screenWidthDp;
widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
nativeAd.setAdUnitId(context.getString(R.string.dummy_ad_unit_id));
nativeAd.setAdSize(new AdSize(widthInDP, 100));
nativeAd.loadAd(adRequest);
adRequest 最初声明如下:
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("DD8112531D2B704F33C3B0314744E92B")
.addTestDevice("62F4961171DF9F253BC3639F7B9A686B")
.build();
我遵循了 Firebase AdMob 官方文档中解释的教程。为什么在动态设置广告尺寸时无法在 NativeExpressAdView 中展示广告?
添加这行代码,您的广告应该不错
xmlns:ads="http://schemas.android.com/apk/res-auto"
所以它变成这样并删除广告尺寸
<com.google.android.gms.ads.NativeExpressAdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/nativeAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="320x150"
android:layout_gravity="center"/>
当我以编程方式添加 NativeExpressAdView 并将其从 XML 中删除时已解决,如下所示。
nativeAd = new NativeExpressAdView(this);
int widthInDP = context.getResources().getConfiguration().screenWidthDp;
widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
nativeAd.setAdSize(new AdSize(widthInDP, 250));
nativeAd.setAdUnitId("myAdUnitId");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Add the NativeExpressAdView to the view hierarchy.
layout.addView(nativeAd);
// Start loading the ad.
nativeAd.loadAd(adRequestBuilder.build());
我正在尝试使用 NativeExpressAdView 但它显示的不是广告
Required XML attribute 'adSize' was missing
黑色背景,红色边框框。我像这样以编程方式将 adUnit 和 adSize 添加到 adView
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/nativeAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
Java代码:
nativeAd = (NativeExpressAdView) itemView.findViewById(R.id.nativeAdView);
int widthInDP = context.getResources().getConfiguration().screenWidthDp;
widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
nativeAd.setAdUnitId(context.getString(R.string.dummy_ad_unit_id));
nativeAd.setAdSize(new AdSize(widthInDP, 100));
nativeAd.loadAd(adRequest);
adRequest 最初声明如下:
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("DD8112531D2B704F33C3B0314744E92B")
.addTestDevice("62F4961171DF9F253BC3639F7B9A686B")
.build();
我遵循了 Firebase AdMob 官方文档中解释的教程。为什么在动态设置广告尺寸时无法在 NativeExpressAdView 中展示广告?
添加这行代码,您的广告应该不错
xmlns:ads="http://schemas.android.com/apk/res-auto"
所以它变成这样并删除广告尺寸
<com.google.android.gms.ads.NativeExpressAdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/nativeAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="320x150"
android:layout_gravity="center"/>
当我以编程方式添加 NativeExpressAdView 并将其从 XML 中删除时已解决,如下所示。
nativeAd = new NativeExpressAdView(this);
int widthInDP = context.getResources().getConfiguration().screenWidthDp;
widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
nativeAd.setAdSize(new AdSize(widthInDP, 250));
nativeAd.setAdUnitId("myAdUnitId");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Add the NativeExpressAdView to the view hierarchy.
layout.addView(nativeAd);
// Start loading the ad.
nativeAd.loadAd(adRequestBuilder.build());