刚刚更新到支持Android 4.4的新Android SDK Rev 22.3。之后,我发现AdView不再具有setGravity
方法。
我需要这个,因为我正在制作一个Unity3D插件,不能使用XML标记。
编辑:我正在尝试将AdView与BottomLeft,BottomCenter,TopLeft,ect等对齐...所以任何其他方式都可以帮助,因为Android上的Java不是我的典型我使用的环境。
答案 0 :(得分:5)
我找到了解决方案。我以前依赖于setGravity方法。为了得到同样的东西,我这样做:
/// Create a AdView
AdView myAdView = new AdView(MY_ACTIVITY);
myAdView.setAdUnitId(<ADMOB ID>);
myAdView.setAdSize(AdSize.SMART_BANNER);
/// Set the initial frame
FrameLayout.LayoutParams initialFrm = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
MY_ACTIVITY.addContentView(myAdView, initialFrm);
/// ....
/// Now whenever I want to position it on the bottom I do this:
myAdView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
/// To put it back on the top, do this:
myAdView.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.TOP));