我正在使用FrameLayout来保存两个视图,一个在另一个视图之上。一个是AdView,另一个是SDLSurfaceView:https://github.com/kivy/python-for-android/blob/master/src/src/org/renpy/android/SDLSurfaceView.java
基本上,我希望将AdView放置在SDLSurfaceView的前面,但仅在显示实际广告时才可以。在启动应用程序后约30秒后,广告会加载,但是如果我在没有广告展示的情况下点击该广告应在的位置,它仍会打开一个链接。在实际加载AdView之前,如何将SDLSurfaceView保持在AdView的前面?
这是我到目前为止的样本:
mView = new SDLSurfaceView(
this,
mPath.getAbsolutePath());
Hardware.view = mView;
//CREATE WIDGETS LAYOUT PARAMETERS
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
//CREATE ADVIEW
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.setAdSize(AdSize.BANNER);
//ADD LAYOUT PARAMETER TO ADVIEW
adView.setLayoutParams(lp);
//SET AD LISTENER
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
adView.bringToFront();
adView.invalidate();
mView.invalidate();
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
adView.getParent().bringChildToFront(mView);
mView.invalidate();
adView.invalidate();
}
});
//CREATE LAYOUT
FrameLayout layout = new FrameLayout(this);
//SET CONTENT TO LAYOUT
setContentView(layout);
//ADD VIEWS TO LAYOUT
layout.addView(adView);
layout.addView(mView);
//BUILD AD REQUEST
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
我正在使用kivy创建一个adroid应用程序,并使用python-for-android来打包apk。该示例是python-for-android打包程序使用的我的PythonActivity.java的一部分。
最佳答案
您可以在广告加载时显示广告,并在广告未加载或显示时从布局中删除。在这种情况下,您必须将adview放置在SDLSurfaceView的顶部并设置其可见性模式:
我会这样:
mView = new SDLSurfaceView(
this,
mPath.getAbsolutePath());
Hardware.view = mView;
//CREATE WIDGETS LAYOUT PARAMETERS
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
//CREATE ADVIEW
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.setAdSize(AdSize.BANNER);
adView.setVisibility(View.GONE);
//ADD LAYOUT PARAMETER TO ADVIEW
adView.setLayoutParams(lp);
//SET AD LISTENER
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
adView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
adView.setVisibility(View.GONE);
}
});
//CREATE LAYOUT
FrameLayout layout = new FrameLayout(this);
//SET CONTENT TO LAYOUT
setContentView(layout);
//ADD VIEWS TO LAYOUT
layout.addView(mView);
layout.addView(adView);
//BUILD AD REQUEST
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);