我有一个libgdx游戏,我成功地将广告放入其中,但我有一个问题
广告涵盖了屏幕的底部,我有一些重要的东西
我的问题是如何将两个视图(gameView和adView)放在一起以便它们不会相互重叠?
这是AndroidLauncher类
public class AndroidLauncher extends AndroidApplication implements AdsController {
protected AdView adView;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new GameMain(this), config);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setVisibility(View.INVISIBLE);
adView.setBackgroundColor(0xff000000);
adView.setAdUnitId("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, adParams);
setContentView(layout);
}
@Override
public void showBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
adView.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
adView.loadAd(builder.build());
}
});
}
@Override
public void hideBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
adView.setVisibility(View.INVISIBLE);
}
});
}
@Override
public boolean showAds() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return (ni != null && ni.isConnected());
}
}
这是AdsController接口
public interface AdsController {
public void showBannerAd();
public void hideBannerAd();
public boolean showAds();
}
如果没有互联网连接(没有广告),我希望我的游戏能够采用全屏尺寸。
我该怎么做?
答案 0 :(得分:0)
您的游戏视图需要将其设置在AdView上方的布局参数。因此,它需要RelativeLayout.LayoutParams,而不仅仅是ViewGroup.LayoutParams。通过这种布局,您可以通过将AdView的可见性设置为GONE
而不是INVISIBLE
来使游戏填满整个屏幕。因此,您可以将其留下,直到广告可用。
要相对于其他视图进行设置,您需要为其他视图提供一个ID,您可以使用View.generateViewId()
生成该ID。
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new GameMain(this), config);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setVisibility(View.GONE);
adView.setBackgroundColor(0xff000000);
adView.setAdUnitId("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
int adViewId = View.generateViewId();
adView.setId(adViewId);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
RelativeLayout.LayoutParams gameParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
gameParams.addRule(RelativeLayout.ABOVE, adViewId);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(adView, adParams);
layout.addView(gameView, gameParams);
setContentView(layout);
}