缺少NativeExpressAdView XML属性adSize

时间:2016-11-13 14:08:37

标签: android xml firebase admob google-play-services

我正在尝试使用NativeExpressAdView而不是它显示的广告

  

必需的XML属性' adSize'失踪

在黑色背景中,红色边框。我正在以编程方式将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中展示广告?

2 个答案:

答案 0 :(得分:3)

添加此行代码,您的广告应该是好的

xmlns:ads="http://schemas.android.com/apk/res-auto"

所以它变成了这样,并取消了adSize

    <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"/>

答案 1 :(得分:1)

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