Android AdMob 横幅添加到 Canvas 之上?

Android AdMob Banner add on top of Canvas?(Android AdMob 横幅添加到 Canvas 之上?)

本文介绍了Android AdMob 横幅添加到 Canvas 之上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Canvas 创建了一个小游戏,现在我想在 Canvas 上展示 AdMob 广告.AdMob 网站上的示例建议我执行以下操作:

I have created a small game using Canvas, and now I want to display AdMob ads on top of the canvas. The Example on the AdMob site suggests I do the following:

 // Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);

// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);

但是,我将 contentView 设置为我创建的自定义画布,如下所示:

However, I am setting the contentView to a Custom Canvas that I created as follows:

setContentView(new CanvasView(this));

画布视图:

public class CanvasView extends View {
    public CanvasView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
...

如何在自定义画布上展示 AdMob 广告?

How can I display AdMob ads on top of my Custom Canvas?

推荐答案

您必须将包含广告和画布的父布局设置为内容视图.修改你的代码如下:

You'd have to set the parent layout containing the ad and canvas as the content view. Modify your code as follows:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));
    // Create an ad request.
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
      .addTestDevice(TestDeviceID)
      .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);

    //Optional - Request full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    //Create canvas view
   View myView = new CanvasView(this);

  //define ad view parameters
 RelativeLayout.LayoutParams adParams = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

    layout.addView(myView);
    layout.addView(adView, adParams);

    //Set main renderer             
    setContentView(layout);

}

这篇关于Android AdMob 横幅添加到 Canvas 之上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android AdMob 横幅添加到 Canvas 之上?

基础教程推荐