设置广告宽度以匹配父项

qij5mzcb  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(389)

所以我的应用程序里有广告,我不想让它们的宽度变大 match_parent . 如果我把它设为 match_parent 它仍然显示为 wrap_content ,如果我像这样在java文件中设置它 adView.setAdSize(AdSize.SMART_BANNER); 我收到一条消息: 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\u banner,我会得到上面提到的错误,即使我删除了它 ads:adSize="BANNER" 来自xml代码的行。

oxosxuxt

oxosxuxt1#

请试试这个

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

但请记住,如果广告客户提供了所有正确的资源,那么只有你的广告将是可见的宽度匹配

ql3eal8s

ql3eal8s2#

你可以使用全宽

ads:adSize="FULL_WIDTHx50"

相关问题