我还能在 XML 中使用 smart_banner 吗?

Can I still use smart_banner in XML?

admob smart_banner 在 java 代码中已弃用。但在 XML.

中未标记为已弃用

我的xml:

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="ca-app-pub-xxx">
</com.google.android.gms.ads.AdView>

我的java:

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

我的问题是,smart_banner 行得通吗?我必须更改我的广告代码吗?难道我做错了什么? 谢谢朋友

Accordingly you are not doing anything wrong , smart banner ads are valid ads size ,but the site says you should rather use Adaptive Banner 相应地更改横幅大小并且更受欢迎。

Adaptive banners are the next generation of responsive ads, maximizing performance by optimizing ad size for each device. Improving on smart banners, which only supported fixed heights, adaptive banners let developers specify the ad-width and use this to determine the optimal ad size.

您的 java 代码显示 AdSize.SMART_BANNER 已弃用,因为 docs says to use

public static AdSize getCurrentOrientationAnchoredAdaptiveBannerAdSize (Context context, int width) 代替 AdSize.SMART_BANNER ,它 returns 具有给定宽度和 Google 优化高度的 AdSize 来创建横幅广告 |这在 xml 中不会发生,因为没有其他方法,您也应该使用以类似方式工作的自适应横幅。

如果您想要使用它,您可以使用这种设置 adSize

的方式
AdSize adSize = getAdSize();
adView.setAdSize(adSize);

并获取 adSize() 作为

private AdSize getAdSize() { 
 Display display = getWindowManager().getDefaultDisplay();
 DisplayMetrics outMetrics = new DisplayMetrics(); 
display.getMetrics(outMetrics);
 float widthPixels = outMetrics.widthPixels; 
float density = outMetrics.density; int adWidth = (int) (widthPixels / density);

 return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth); }