CardView中的NativeExpressAdView

时间:2016-11-11 21:29:11

标签: android admob native-ads

<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存在问题。有没有办法做到这一点?显示使用的FULL_WIDTH添加和CardView上的一些边距。我也尝试在父级LinearLayout上设置填充和边距,而不是在CardView上使用相同问题的边距。

1 个答案:

答案 0 :(得分:1)

FULL_WIDTH常量指定广告应占据设备屏幕的整个宽度。在您的情况下,广告没有足够的空间,因为它的一些祖先引入了额外的水平填充。为了实现目标,您可以确定运行时宽度应该是多少。您需要做的就是计算设备的宽度并减去正确的填充量。例如:

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

重要的是要注意这里的值是倾角(与密度无关的像素),而不是像素。