我正在使用Andengine并希望添加Admob横幅。我找到了这个代码,它运行良好。但是使用此代码,LayoutParams需要API 19作为最低要求。我希望变得更低,比如8或类似的东西。我可以用什么呢?
@Override protected void onSetContentView() {
// CREATING the parent FrameLayout //
final FrameLayout frameLayout = new FrameLayout(this);
// CREATING the layout parameters, fill the screen //
final FrameLayout.LayoutParams frameLayoutLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
// CREATING a Smart Banner View //
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-2181015300018417/9550656088");
// Doing something I'm not 100% sure on, but guessing by the name //
adView.refreshDrawableState();
adView.setVisibility(AdView.VISIBLE);
// ADVIEW layout, show at the bottom of the screen //
final FrameLayout.LayoutParams adViewLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
// REQUEST an ad (Test ad) //
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("AB38B36BA72798C94A9C9329007FD4B0")
.build();
adView.loadAd(adRequest);
// RENDER the add on top of the scene //
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine, this);
// SURFACE layout ? //
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
// ADD the surface view and adView to the frame //
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
// SHOW AD //
this.setContentView(frameLayout, frameLayoutLayoutParams);
} // onSetContentView()//
的结尾答案 0 :(得分:1)
问题是FrameLayout.LayoutParams构造函数 不支持 另一个FrameLayout作为参数,直到 api 19 。
然而还有另外两个签名:
public FrameLayout.LayoutParams(ViewGroup.LayoutParams source) - api 1
public FrameLayout.LayoutParams(ViewGroup.MarginLayoutParams source) - api 1
FrameLayout.LayoutParams是ViewGroup.MarginLayoutParams子类
所以在检查参数布局是FrameLayout.LayoutParams
之后您可以解决以下问题:
if(super.createSurfaceViewLayoutParams() instanceOf FrameLayout.LayoutParams){
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams((ViewGroup.MarginLayoutParams)super.createSurfaceViewLayoutParams());
}
第一个签名(API 19)之间的差异 和别的 是新的LayoutParams对象不会设置重力
if(super.createSurfaceViewLayoutParams() instanceOf FrameLayout.LayoutParams){
ViewGroup.MarginLayoutParams paramLP=(ViewGroup.MarginLayoutParams)super.createSurfaceViewLayoutParams();
int gravityHolder=((FrameLayout.LayoutParams)super.createSurfaceViewLayoutParams()).gravity;
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(paramLP);
surfaceViewLayoutParams.gravity=gravityHolder;
}
答案 1 :(得分:0)
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
我的工作如下:
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
android.widget.FrameLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.MATCH_PARENT);
surfaceViewLayoutParams.gravity = Gravity.CENTER;
似乎不适用于Android Studio + Andengine,API19。