当用户到达屏幕时,我尝试在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;
}
}
}
};
代码。也许我需要以另一种方式控制方向,但我不知道如何。提前感谢您的帮助!
答案 0 :(得分: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);