大部分Libgdx的安卓入口里,都是直接一个DirectedGame来初始化:initialize(new GameDirectedGame(), cfg);
而一些广告需要用自定义布局潜入一个View。Libgdx安卓入口中,MainActivity(继承AndroidApplication),oncreate方法里用以下方法可实现。
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
// 创建布局
RelativeLayout layout = new RelativeLayout(instance);
// 创建 libgdx View (DirectedGame)
View gameView = initializeForView(new GameMain(app), cfg);
// 添加view
layout.addView(gameView);
//初始化广告视图
BaiduBanner banner = new BaiduBanner(instance);
//广告布局设置,根据自己的位置来设置参数
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
(int) (getWindowManager().getDefaultDisplay().getWidth() * 0.55f),
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
adParams.rightMargin = getWindowManager().getDefaultDisplay().getWidth() / 4;
layout.addView(banner, adParams);
//设置嵌套后的布局
setContentView(layout);