我想用NativeExpressAdview,但它没有显示广告
缺少必需的XML属性“adSize”
黑色背景,红色边框。我在adview中添加adunit和adsize,程序如下

    <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中显示广告?

最佳答案

当我以编程方式添加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());

关于android - 缺少NativeExpressAdView XML属性adSize,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40574719/

10-10 01:12