如何在放置在布局内时将SDLSurfaceView置于前面?

时间:2014-12-11 05:25:43

标签: java android admob kivy

我正在使用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 packager使用的PythonActivity.java的一部分。

1 个答案:

答案 0 :(得分:1)

您可以在加载广告时展示广告,并在广告未加载或显示时从广告框中删除。在这种情况下,您必须将广告视图置于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);