3

当用户到达屏幕时,我尝试在 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;
                 }
             }
         }
     };

代码。也许我需要以另一种方式控制方向,但我不知道如何。提前感谢您的帮助!

4

1 回答 1

3

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);
于 2012-07-12T22:46:29.813 回答