将广告宽度设置为match_parent

时间:2016-03-28 12:54:55

标签: java android xml ads

所以我的应用中有广告,我的宽度不是match_parent。如果我将其设置为match_parent,它仍显示为wrap_content,如果我将其设置为此文件adView.setAdSize(AdSize.SMART_BANNER);的java文件,则会收到此消息的崩溃:Caused by: java.lang.IllegalStateException: The ad size can only be set once on AdView

以下是我广告的xml和java:

 AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();

        adView.loadAd(adRequest);



   <com.google.android.gms.ads.AdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/adView"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
    </com.google.android.gms.ads.AdView>

如果我将其设置为SMART_BANNER,即使我从xml代码中删除了ads:adSize="BANNER"行,我也会收到上面提到的错误。

2 个答案:

答案 0 :(得分:0)

请试试这个

<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        ads:adUnitId="@string/banner_ad_unit_id"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub_id"
        ads:loadAdOnCreate="true"/>

但请注意,如果广告客户提供了所有正确的资源,那么只有您的广告在match_patent的宽度中可见

答案 1 :(得分:0)

您可以使用FULL_WIDTH

ads:adSize="FULL_WIDTHx50"