2016-11-11 60 views
2
<LinearLayout 
    orientation="vertical"> 
... 
    <android.support.v7.widget.CardView 
     android:id="@+id/card_pitch" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     card_view:cardCornerRadius="0dp"> 

     <com.google.android.gms.ads.NativeExpressAdView 
      android:id="@+id/adview_small_light" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:visibility="gone" 
      ads:adSize="FULL_WIDTHx100" 
      ads:adUnitId="xxx"> 
     </com.google.android.gms.ads.NativeExpressAdView> 

    </android.support.v7.widget.CardView> 
1-11 16:21:22.211 28899-28899/? W/Ads: Not enough space to show ad. Needs 411x100 dp, but only has 379x385 dp. 
11-11 16:21:22.211 28899-28899/? W/Ads: Not enough space to show ad. Needs 411x100 dp, but only has 379x100 dp. 
11-11 16:21:22.211 28899-28899/? W/Ads: Not enough space to show ad. Needs 411x100 dp, but only has 379x173 dp. 
11-11 16:21:22.211 28899-28899/? W/Ads: Not enough space to show ad. Needs 411x100 dp, but only has 379x100 dp. 

廣告不顯示,在CardView邊緣有FULL_WIDTH問題。有沒有辦法做到這一點?在CardView上顯示使用FULL_WIDTH和一些邊距的添加。我也嘗試在父視圖上使用填充和邊距,而不是在CardView上使用邊距來解決同樣的問題。NativeExpressAdView在CardView

回答

1

FULL_WIDTH常量指定廣告應占據設備屏幕的整個寬度。在你的情況下,廣告沒有足夠的空間,因爲它的一些祖先引入了額外的水平填充。爲了實現您的目標,您可以確定運行時的寬度。您只需計算設備的寬度並減去正確的填充量即可。例如:

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); 
width = displayMetrics.widthPixels/displayMetrics.density; 
adView.setAdSize(new AdSize(width - padding, 100)); 

重要的是要注意,這裏的值是dip(密度獨立像素),而不是像素。