【问题标题】:Admob Adaptive Banner ad implementationAdmob 自适应横幅广告实施
【发布时间】:2021-01-06 10:09:57
【问题描述】:

我正在用新的自适应横幅广告替换我当前的横幅广告。自适应广告尺寸是在捕获宽度尺寸后计算的。我将 adview 的 framelayout 放在布局的底部。

<Relativelayout>
..
//// linear layout above ad_view_container   
<FrameLayout
        android:id="@+id/ad_view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true" />

</Relativelayout>

但现在的问题是,首先加载整个页面,然后在 1 -2 秒后,整个布局向上移动并显示 adative 横幅广告。

那么这是否违反了 admob 政策?这种情况应该如何处理,我应该为自适应广告设置高度,这样布局就不会发生上移?搜索了很多,但没有找到答案。提前致谢。

【问题讨论】:

  • adView固定高度
  • 但是怎么做呢?自适应横幅高度是动态决定的。根据文档,我们没有可以给出的最小高度
  • 能否再添加一些布局代码?
  • 我所做的是先猜测高度以减少闪烁并将广告高度存储在 SharedPrefs 中。根据文档,如果宽度参数也保持不变,那么给定设备的自适应高度是不变的,这是通常的事情。然后,您可以创建一个占位符,广告将替换该占位符而不闪烁。

标签: android admob banner-ads


【解决方案1】:

我不确定,如果您的唯一意图是询问这是否违反政策。

但为了防止布局在一段时间后发生变化,我使用以下实现。 当您像我一样有更复杂的布局组合时,它也会有所帮助。我使用ConstraintLayout 在纵向模式下将我的屏幕分成两部分。横幅位于这两个部分之一中,它们之间的比例不是固定的,而是取决于某些逻辑。因此,此实现也适用于此要求,因为它会覆盖 onMeasure 以根据可用宽度确定最佳尺寸。

public class AdBanner extends FrameLayout
{
    private AdView mAdView;
    private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();

    public AdBanner(Context context)
    {
        super(context);
    }

    public AdBanner(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public AdBanner(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (!isShowBanner())
        {
            super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST));
            return;
        }
        int width = MeasureSpec.getSize(widthMeasureSpec);
        if (width <= 0)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // That's where we determine the most accurate banner format.
        AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(getContext(), getDpWidth(width));
        int height = adSize.getHeightInPixels(getContext());
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode != MeasureSpec.UNSPECIFIED)
        {
            height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
        }
        setMeasuredDimension(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.getMode(heightMeasureSpec)));
    }

    protected int getDpWidth(int width)
    {
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        display.getMetrics(mDisplayMetrics);
        return (int) (width / mDisplayMetrics.density);
    }

    protected boolean isShowBanner()
    {
        // Do your checks here, like whether the user payed for ad removement.
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        if (!isShowBanner())
        {
            return;
        }
        int width = r - l;
        if (width <= 0)
        {
            return;
        }
        AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(getContext(), getDpWidth(width));

        // Prevent the ad from beeing added with each layout cicle,
        // by checking, whether or not available size actually changed the format of the banner
        if (mAdView == null || !adSize.equals(mAdView.getAdSize()))
        {
            removeAllViews();
            mAdView = new AdView(getContext());
            mAdView.setAdSize(adSize);
            ((GameApplication) getContext().getApplicationContext()).androidInjector().getAdService().loadBannerAd(getRootActivity(this), mAdView);
            this.addView(mAdView);
        }
        mAdView.layout(0, 0, width, b - t);
    }
}

【讨论】:

    【解决方案2】:

    为此,您可以使用以下代码

    public class MainActivity extends AppCompatActivity {
    
    private FrameLayout adContainerView;
    private AdView adView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Initialize the Mobile Ads SDK.
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) { }
        });
        adContainerView = findViewById(R.id.ad_view_container);
        // Step 1 - Create an AdView and set the ad unit ID on it.
        adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        adContainerView.addView(adView);
        loadBanner();
    }
    
    private void loadBanner() {
        // Create an ad request. 
        AdRequest adRequest =
                new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                        .build();
    
        AdSize adSize = getAdSize();
        // Step 4 - Set the adaptive ad size on the ad view.
        adView.setAdSize(adSize);
    
        // Step 5 - Start loading the ad in the background.
        adView.loadAd(adRequest);
    }
    
    private AdSize getAdSize() {
        // Step 2 - Determine the screen width (less decorations) to use for the ad width.
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
    
        float widthPixels = outMetrics.widthPixels;
        float density = outMetrics.density;
    
        int adWidth = (int) (widthPixels / density);
    
        // Step 3 - Get adaptive ad size and return for setting on the ad view.
        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
    }
    }
    

    【讨论】:

    • 嗨 Mehul,感谢您的回复。我已经添加了这段代码。但出现的问题首先是我的整个布局是可见的,并且在 1 或 2 秒后弹出广告。当广告弹出时,我的整个布局向上移动。我认为这违反了 admob 政策
    猜你喜欢
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多