在 SurfaceView 上居中 MoPub 横幅

发布于 2024-11-19 11:06:42 字数 785 浏览 2 评论 0原文

我想在我的 Android 游戏中添加 MoPub 广告。游戏是在 SurfaceView 类上创建的。我没有 xml 布局文件。问题是我无法使广告在屏幕上居中。我尝试一切重心和重心。还是不行。 MoPubView 扩展了 FrameLayout。

    MoPubView mAdView = new MoPubView(this);
    mAdView.setAdUnitId("_MY__ID_"); 
    mAdView.loadAd();

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

    RelativeLayout mRelativeLayout = new RelativeLayout(this);

    mRelativeLayout.setGravity(Gravity.CENTER_HORIZONTAL);
    mRelativeLayout.addView(mSurface);
    mRelativeLayout.addView(mAdView, layoutParams);

    mRelativeLayout.invalidate();
    setContentView(mRelativeLayout);

请给我一些建议,因为它让我发疯。我觉得我错过了一些简单的事情。

I'd like add MoPub ad in my Android game. Game is created on SurfaceView Class. I have no xml layout file. The problem is that I can't center ad on screen. I try center and gravity everything. Still doesn't work. MoPubView extends FrameLayout.

    MoPubView mAdView = new MoPubView(this);
    mAdView.setAdUnitId("_MY__ID_"); 
    mAdView.loadAd();

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

    RelativeLayout mRelativeLayout = new RelativeLayout(this);

    mRelativeLayout.setGravity(Gravity.CENTER_HORIZONTAL);
    mRelativeLayout.addView(mSurface);
    mRelativeLayout.addView(mAdView, layoutParams);

    mRelativeLayout.invalidate();
    setContentView(mRelativeLayout);

Please advise me something because it drawing me crazy. I feel I'm missing something easy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骄兵必败 2024-11-26 11:06:42

您无法将内容直接集中在 MoPub 中,或者至少我还没有找到这样做的方法。它基于 FrameLayout。

您必须将 HTML 广告居中。如果您在 MoPub 活动中使用自定义 HTML 代码,只需将代码封装在

...

内即可。 (通过 MoPub 管理网络执行此操作)。它解决了我的问题,我正在使用 OpenX 广告提供商。

You can't center the content directly in MoPub, or at least I haven't found a way to do so. It's based on FrameLayout.

You have to center the ad in HTML. If you are using custom HTML code inside your MoPub campaign, just wrap the code inside <div style="text-align:center">...</div>. (Do this through the MoPub administration web). It solved my problem, I was using an OpenX ad provider.

风启觞 2024-11-26 11:06:42

只需使用“FILL_PARENT”参数创建一个容器布局,然后将广告视图添加到其中,如下所示:

MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(XXXXXXX);

RelativeLayout adContainer = new RelativeLayout(this);
addContentView(adContainer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

LayoutParams adParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1); //to align vertically at top
adParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1); //to align horizontally at center

adContainer.addView(mAdView, adParams);
mAdView.loadAd();

Just create a container layout with "FILL_PARENT" parameters and add your ad view into it like this:

MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(XXXXXXX);

RelativeLayout adContainer = new RelativeLayout(this);
addContentView(adContainer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

LayoutParams adParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1); //to align vertically at top
adParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1); //to align horizontally at center

adContainer.addView(mAdView, adParams);
mAdView.loadAd();
枫林﹌晚霞¤ 2024-11-26 11:06:42

我知道设置恒定的横幅宽度不是一个好主意,但只要您的横幅为 320 像素,它就可以正常工作:

final float scale = getResources().getDisplayMetrics().density;

MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(MOPUB_ID); 
mAdView.loadAd();

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (320*scale), LayoutParams.WRAP_CONTENT);

layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

RelativeLayout mRelativeLayout = new RelativeLayout(this);

mRelativeLayout.addView(mSurface);
mRelativeLayout.addView(mAdView, layoutParams);

mRelativeLayout.invalidate();
setContentView(mRelativeLayout);

I know it is not good idea to set constant banner width but as long as your banners are 320px it works fine:

final float scale = getResources().getDisplayMetrics().density;

MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(MOPUB_ID); 
mAdView.loadAd();

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (320*scale), LayoutParams.WRAP_CONTENT);

layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

RelativeLayout mRelativeLayout = new RelativeLayout(this);

mRelativeLayout.addView(mSurface);
mRelativeLayout.addView(mAdView, layoutParams);

mRelativeLayout.invalidate();
setContentView(mRelativeLayout);
高冷爸爸 2024-11-26 11:06:42

我用下面的代码片段做了同样的事情。我正在使用 LibGDX java 游戏引擎。

Relativelayout layout = new RelativeLayout(this);
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);

                moPubView = new MoPubView(mActivity.getApplicationContext());
                moPubView.setAdUnitId("XXXXXX");// Banner test Ad unit Id.
                moPubView.setAutorefreshEnabled(true);
                moPubView.setVisibility(View.GONE);
                layout.addView(moPubView,adParams);

1.我在父级中有相对布局。

2.在父相对布局中添加MoPubView,仅使用layoutParams ALIGN_PARENT_BOTTOM && CENTER_HORIZONTAL

layout.addView(moPubView,adParams);

I have done same thing with below code snippet.I am using LibGDX java game engine.

Relativelayout layout = new RelativeLayout(this);
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);

                moPubView = new MoPubView(mActivity.getApplicationContext());
                moPubView.setAdUnitId("XXXXXX");// Banner test Ad unit Id.
                moPubView.setAutorefreshEnabled(true);
                moPubView.setVisibility(View.GONE);
                layout.addView(moPubView,adParams);

1.I have Relative layout in parent.

2.Added MoPubView in the parent Relative layout with layoutParams only ALIGN_PARENT_BOTTOM && CENTER_HORIZONTAL

layout.addView(moPubView,adParams);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文