2016-11-13 94 views
1

我試圖用NativeExpressAdView但不是廣告它顯示NativeExpressAdView XML屬性adSize時失蹤

必需的XML屬性「adSize時」失蹤

在黑色的背景

,紅色邊框框。我加入廣告單元和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中動態地設置廣告尺寸時展示廣告?

回答

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()); 
+0

三文魚嗎?適用於我。 –

3

加入這行代碼,你的廣告應該是不錯的

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

所以它成了這個樣子,並decleare的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"/> 
+0

我也試過這個命名空間。另外,我想動態設置adView的大小。 –

相關問題