【问题标题】:admob ad banner overlapping my game screen androidadmob 广告横幅与我的游戏屏幕 android 重叠
【发布时间】:2015-10-11 23:20:58
【问题描述】:

我成功集成了 admob 广告,但是我在将横幅广告放置在游戏屏幕下而不重叠我的游戏视图时遇到了问题我目前的问题就像图片Pic 1 我希望它像这张图片一样

谢谢 Cute Baby Girl Names Pic2

注意:我使用 java admob 代码这是我的代码

package com.xxxx.xxxxx;
import org.cocos2d.actions.CCActionManager;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSpriteFrameCache;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.ideastudio.incroyableAventuresAlaadin.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

public class xxxxxx extends Activity{
/** Called when the activity is first created. */

public static xxxxx app;

public static CCGLSurfaceView mGLSurfaceView;
private boolean isCreated = false; 

private static final String AD_UNIT_ID = "ca-app-pub-xxxxxxxx";
private static final String AD_INTERSTITIAL_UNIT_ID = "ca-app-pub-xxxxx";

/** The interstitial ad. */
private InterstitialAd interstitialAd;
private AdView adView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    if( !isCreated ){
        isCreated = true;
    } else {
        return;
    }
    
    app = this;

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    
    
    mGLSurfaceView = new CCGLSurfaceView(this);
    
    
    // Create the adView
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the adView to it
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM ,RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    adView.setLayoutParams(params);
    
    layout.addView(mGLSurfaceView);
    layout.addView(adView);
    
    setContentView(layout);
    
    
    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

    //-----------------------------------------------------Interstitial Add
    // Create an Interstitial ad.
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_INTERSTITIAL_UNIT_ID);
     // Load the interstitial ad.
    AdRequest interstitialAdRequest = new AdRequest.Builder().build();
    interstitialAd.loadAd(interstitialAdRequest);
    
    /////////////////////////////////////////////////////////////////////////////
    Common.game_initialize();
    getScaledCoordinate();
    CCDirector.sharedDirector().attachInView(mGLSurfaceView); 

    // attach the OpenGL view to a window
    Common.sound_engine = SoundEngine.sharedEngine();
    loadSound();

    CCScene scene = CCScene.node();
    scene.addChild(new HelloWorldLayer(), 1);
    CCDirector.sharedDirector().runWithScene(scene);
}

@Override
public void onStart() {
    super.onStart();
}

@Override    
public void onPause() {
    if (adView != null) {
          adView.pause();
        }
    
    
    super.onPause();

    MediaGlobal._shared().pauseMusic();

    if(GameLayer.sharedGameLayer() != null){
        GameLayer.sharedGameLayer().onPause(null);
    }
    CCDirector.sharedDirector().pause();
}

@Override
public void onResume() {
    super.onResume();

    if (adView != null) {
        adView.resume();
      }
    
    MediaGlobal._shared().resumeMusic();
}

@Override
public void onDestroy() {
    
    if (adView != null) {
          adView.destroy();
        }
        
    
    isCreated = false;

    MediaGlobal._shared().stopMusic();
    Common.sound_engine.realesAllEffects();

    super.onDestroy();
    CCDirector.sharedDirector().end();       

    CCTextureCache.sharedTextureCache().removeAllTextures();
    CCTextureCache.sharedTextureCache().removeAllTextures();
    CCSpriteFrameCache.sharedSpriteFrameCache().removeAllSpriteFrames();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        exitGameDialog();
        return false;
    }
    return super.onKeyDown(keyCode, event);
}

public void exitGameDialog() {
    Builder builder = new AlertDialog.Builder(FlyingPanda.this)
            .setIcon(R.drawable.icon)
            .setTitle("Quitter le jeu?")
            .setMessage("Est-vous sûr?")
            .setNegativeButton("Non", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            })
            .setPositiveButton("Oui",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            CCActionManager.sharedManager()
                                    .removeAllActions();
                            CCDirector.sharedDirector().end();
                            finish();
                        }
                    });
    builder.create().show();
}

private void loadSound() {
    SoundEngine.purgeSharedEngine();

    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.bomb);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.bounce);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.death);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.fly);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.gamebg);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.gameover);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.jumppad);
}

private void getScaledCoordinate() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    Common.SCREEN_WIDTH = displayMetrics.widthPixels;
    Common.SCREEN_HEIGHT = displayMetrics.heightPixels;
    Common.kXForIPhone = Common.SCREEN_WIDTH / 480.0f;
    Common.kYForIPhone = Common.SCREEN_HEIGHT / 320.0f;
}


// Admob Setting
////////////////////////////////////////////////////////////////////////////////

public void setHideAdView(final boolean bHide) {
    runOnUiThread(new Runnable() {
        public void run() {
            if(bHide) {
                adView.setVisibility(View.INVISIBLE);
            }
            else {
                adView.setVisibility(View.VISIBLE);
            }
        }
    });
    
    
}

public void showInterstitialAds()
{
    runOnUiThread(new Runnable() {
        public void run() {

            // Load the interstitial ad.
            AdRequest interstitialAdRequest = new AdRequest.Builder().build();
            interstitialAd.loadAd(interstitialAdRequest);
            
            interstitialAd.show();
        }
    });
}

  }

【问题讨论】:

    标签: java android google-play admob google-play-services


    【解决方案1】:

    您需要为广告视图添加规则,将其设置为“低于”gl 表面视图。

    int id = 173; //random number
    mGLSurfaceView.setId(id);
    params.addRule(RelativeLayout.BELOW, id);
    

    如果使用 API 级别 17+,您可以使用:

    View.generateViewId()
    

    或者在 xml 中保留那个号码。

    【讨论】:

    • 你能告诉我我把这段代码放在哪里----> int id = 173; //随机数 mGLSurfaceView.setId(id); params.addRule(RelativeLayout.BELOW, id); @Mattias
    • 添加此代码后它不起作用我的横幅消失了..=(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多