其他分享
首页 > 其他分享> > android-如何在Libgdx中更改AdMob方向

android-如何在Libgdx中更改AdMob方向

作者:互联网

当用户到达屏幕时,我试图更改Libgdx中的AdMob方向.我想把它放在屏幕的底部.我做了一个处理程序,在适当的屏幕上实现它,然后通过

myHandler.moveAds(true);

我的界面如下所示:

public interface IActivityRequestHandler {
    public void showAds(boolean show);
    public void moveAds(boolean move);
}

Android项目文件如下所示:

public class MyAndroidnewActivity extends AndroidApplication implements IActivityRequestHandler {
     protected AdView adView;
     RelativeLayout.LayoutParams adParams;

     private final int SHOW_ADS = 1;
     private final int HIDE_ADS = 0;
     private final int MOVE_ADS = 1;
     private final int PLACE_ADS = 0;

     protected Handler handler = new Handler()
     {
         @Override
         public void handleMessage(Message msg) {
             switch(msg.what) {
                 case SHOW_ADS:
                 {
                     adView.setVisibility(View.VISIBLE);
                     break;
                 }
                 case HIDE_ADS:
                 {
                     adView.setVisibility(View.GONE);
                     break;
                 }
             }
         }
     };

     protected Handler moveHandler = new Handler()
     {
         @Override
         public void handleMessage(Message msg) {
             switch(msg.what) {
                 case MOVE_ADS:
                 {
                    //In my opinion this code is not good, because It doesn't do anything 
                    //when I call the myHandler.moveAds(true); in an another class      
                    adView.setGravity(Gravity.BOTTOM);
                     break;
                 }
                 case PLACE_ADS:
                 {
                    adView.setGravity(Gravity.TOP);
                     break;
                 }
             }
         }
     };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;
        cfg.useAccelerometer = false;
        cfg.useCompass = false;


            // Create the layout
            RelativeLayout layout = new RelativeLayout(this);

            // Do the stuff that initialize() would do for you
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

            // Create the libgdx View
            View gameView = initializeForView(new MyApp(this), false);

            // Create and setup the AdMob view
            adView = new AdView(this, AdSize.BANNER, "MYOWNKEY"); // Put in your secret key here
            AdRequest re = new AdRequest();
            re.addTestDevice("MYOWNDEVICECODE");
            adView.loadAd(re);

            // Add the libgdx view
            layout.addView(gameView);

            // Add the AdMob view
            adParams = 
                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            adParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

            layout.addView(adView, adParams);

            // Hook it all up
            setContentView(layout);
        }


        @Override
        public void showAds(boolean show) {
           handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
        }

        @Override
        public void moveAds(boolean move) {
           moveHandler.sendEmptyMessage(move ? MOVE_ADS : PLACE_ADS);
        }
    }

但是当我调用myHandler.moveAds(true);时,方法在另一个类中,它什么也不做,没有错误,只是广告在(横向)屏幕的顶部,而不是底部.

我认为我的问题

  protected Handler moveHandler = new Handler()
     {
         @Override
         public void handleMessage(Message msg) {
             switch(msg.what) {
                 case MOVE_ADS:
                 {
                    //In my opinion this code is not good, because It doesn't do anything 
                    //when I call the myHandler.moveAds(true); in an another class      
                    adView.setGravity(Gravity.BOTTOM);
                     break;
                 }
                 case PLACE_ADS:
                 {
                    adView.setGravity(Gravity.TOP);
                     break;
                 }
             }
         }
     };

码.也许我需要以另一种方式控制方向,但是我不知道如何.在此先感谢您的帮助!

解决方法:

setGravity()方法确定将内容(在本例中为广告)放置在AdView中的位置,而不是将AdView放置在其容器中的位置.

看起来您的容器是一个RelativeLayout,因此您应该能够只设置新的LayoutParams而不是设置引力:

RelativeLayout.LayoutParams adParams =
    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); // or RelativeLayout.ALIGN_PARENT_TOP
adView.setLayoutParams(adParams);

标签:admob,libgdx,android
来源: https://codeday.me/bug/20191201/2078047.html