如何将广告视图与游戏视图分开?
How to separate ads View from Game View?
目前我在 libgdx 中有一款游戏会在游戏布局顶部显示广告。
但是,如您所见,它隐藏了屏幕顶部的一部分,即显示分数的地方。
问题:
我怎样才能让广告显示在游戏上方 view/screen,这样它就不会 overlap/hides 游戏中的任何内容?我希望屏幕如下图所示。
当前代码:
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import de.golfgl.gdxgamesvcs.GpgsClient;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class AndroidLauncher extends AndroidApplication {
private RelativeLayout layout;
private RelativeLayout.LayoutParams params;
private AdView bannerAd;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
//
GpgsClient gpgsClient = new GpgsClient();
gpgsClient.initialize(this, false);
SpaceEscape game = new SpaceEscape(gpgsClient);
//
//initialize(game, config);
View gameView = initializeForView(game,config);
////////// Define the layout
layout = new RelativeLayout(this);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
bannerAd = new AdView(this);
bannerAd.setAdUnitId("REDACTED");
bannerAd.setAdSize(AdSize.BANNER);
layout.addView(bannerAd, params);
setContentView(layout);
AdRequest ad = new AdRequest.Builder().build();
bannerAd.loadAd(ad);
}
}
为了避免这种使用 RelativeLayout
的重叠效果,您可以使用 RelativeLayout.ALIGN_PARENT_TOP
规则在屏幕顶部创建一个 Ad Container
(例如:RelativeLayout 容器)并使用 RelativeLayout.BELOW
规则在广告容器下方添加 GameView
。最后将您的 AdView
添加为上述 Ad Container
.
的子项
下面是如何执行上述结构的示例:
//define the layout
layout = new RelativeLayout(this);
//adView Container RelativeLayout
RelativeLayout adContainerRL = new RelativeLayout(this);
adContainerRL.setBackgroundColor(Color.BLACK);
adContainerRL.setId(ViewCompat.generateViewId());
RelativeLayout.LayoutParams adContainerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adContainerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adContainerRL, adContainerParams);
//adView
AdView bannerAd = new AdView(this);
bannerAd.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //this is a test ad unit id
bannerAd.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
adContainerRL.addView(bannerAd, adViewParams);
//gameView
View gameView = initializeForView(game, config);
RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
gameViewParams.addRule(RelativeLayout.BELOW, adContainerRL.getId());
layout.addView(gameView, gameViewParams);
//set the layout
setContentView(layout);
//load ad
AdRequest ad = new AdRequest.Builder().build();
bannerAd.loadAd(ad);
是的,效果很好,
layout = new RelativeLayout(this);
//adView Container RelativeLayout
RelativeLayout adContainerRL = new RelativeLayout(this);
adContainerRL.setBackgroundColor(Color.BLACK);
adContainerRL.setId(ViewCompat.generateViewId());
RelativeLayout.LayoutParams adContainerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adContainerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adContainerRL, adContainerParams);
//adView
AdView bannerAd = new AdView(this);
bannerAd.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //this is a test ad unit id
bannerAd.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
adContainerRL.addView(bannerAd, adViewParams);
//gameView
View gameView = initializeForView(game, config);
RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
gameViewParams.addRule(RelativeLayout.BELOW, adContainerRL.getId());
layout.addView(gameView, gameViewParams);
//set the layout
setContentView(layout);
//load ad
AdRequest ad = new AdRequest.Builder().build();
bannerAd.loadAd(ad);
目前我在 libgdx 中有一款游戏会在游戏布局顶部显示广告。 但是,如您所见,它隐藏了屏幕顶部的一部分,即显示分数的地方。
问题: 我怎样才能让广告显示在游戏上方 view/screen,这样它就不会 overlap/hides 游戏中的任何内容?我希望屏幕如下图所示。
当前代码:
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import de.golfgl.gdxgamesvcs.GpgsClient;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class AndroidLauncher extends AndroidApplication {
private RelativeLayout layout;
private RelativeLayout.LayoutParams params;
private AdView bannerAd;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
//
GpgsClient gpgsClient = new GpgsClient();
gpgsClient.initialize(this, false);
SpaceEscape game = new SpaceEscape(gpgsClient);
//
//initialize(game, config);
View gameView = initializeForView(game,config);
////////// Define the layout
layout = new RelativeLayout(this);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
bannerAd = new AdView(this);
bannerAd.setAdUnitId("REDACTED");
bannerAd.setAdSize(AdSize.BANNER);
layout.addView(bannerAd, params);
setContentView(layout);
AdRequest ad = new AdRequest.Builder().build();
bannerAd.loadAd(ad);
}
}
为了避免这种使用 RelativeLayout
的重叠效果,您可以使用 RelativeLayout.ALIGN_PARENT_TOP
规则在屏幕顶部创建一个 Ad Container
(例如:RelativeLayout 容器)并使用 RelativeLayout.BELOW
规则在广告容器下方添加 GameView
。最后将您的 AdView
添加为上述 Ad Container
.
下面是如何执行上述结构的示例:
//define the layout
layout = new RelativeLayout(this);
//adView Container RelativeLayout
RelativeLayout adContainerRL = new RelativeLayout(this);
adContainerRL.setBackgroundColor(Color.BLACK);
adContainerRL.setId(ViewCompat.generateViewId());
RelativeLayout.LayoutParams adContainerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adContainerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adContainerRL, adContainerParams);
//adView
AdView bannerAd = new AdView(this);
bannerAd.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //this is a test ad unit id
bannerAd.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
adContainerRL.addView(bannerAd, adViewParams);
//gameView
View gameView = initializeForView(game, config);
RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
gameViewParams.addRule(RelativeLayout.BELOW, adContainerRL.getId());
layout.addView(gameView, gameViewParams);
//set the layout
setContentView(layout);
//load ad
AdRequest ad = new AdRequest.Builder().build();
bannerAd.loadAd(ad);
是的,效果很好,
layout = new RelativeLayout(this);
//adView Container RelativeLayout
RelativeLayout adContainerRL = new RelativeLayout(this);
adContainerRL.setBackgroundColor(Color.BLACK);
adContainerRL.setId(ViewCompat.generateViewId());
RelativeLayout.LayoutParams adContainerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adContainerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adContainerRL, adContainerParams);
//adView
AdView bannerAd = new AdView(this);
bannerAd.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //this is a test ad unit id
bannerAd.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
adContainerRL.addView(bannerAd, adViewParams);
//gameView
View gameView = initializeForView(game, config);
RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
gameViewParams.addRule(RelativeLayout.BELOW, adContainerRL.getId());
layout.addView(gameView, gameViewParams);
//set the layout
setContentView(layout);
//load ad
AdRequest ad = new AdRequest.Builder().build();
bannerAd.loadAd(ad);