Android AdMob Banner add on top of 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 之上?
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01