我创建了Cordova项目并在屏幕右侧显示广告,为此,我设置了相对布局重力。活动是横向模式。
RelativeLayout.ALIGN_PARENT_END
但是它不起作用。
我想在屏幕上直接显示广告。
我使用了MoPub Cordova插件
这是我的代码
public void showBanner(final int Argos, final int argX, final int argY) {
Log.d("GenericAdPlugin", "showBanner " + argX + "::" + argY);
if (this.adView == null) {
Log.e("GenericAdPlugin", "banner is null, call createBanner() first.");
} else {
boolean bannerAlreadyVisible = this.bannerVisible;
final Activity activity = this.getActivity();
activity.runOnUiThread(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressLint("WrongConstant")
public void run() {
View mainView = GenericAdPlugin.this.getView();
Log.d(LOGTAG, " MainView : Height " + mainView.getHeight() + " Width " + mainView.getWidth());
if (mainView == null) {
Log.e("GenericAdPlugin", "Error: could not get main view");
} else {
Log.d("GenericAdPlugin", "webview class: " + mainView.getClass());
if (GenericAdPlugin.this.bannerVisible) {
GenericAdPlugin.this.detachBanner();
}
int bw = GenericAdPlugin.this.__getAdViewWidth(GenericAdPlugin.this.adView);
int bh = GenericAdPlugin.this.__getAdViewHeight(GenericAdPlugin.this.adView);
Log.d("GenericAdPlugin", String.format("show banner: (%d x %d)", bw, bh));
ViewGroup rootView = (ViewGroup) mainView.getRootView();
int rw = rootView.getWidth();
int rh = rootView.getHeight();
Log.d(LOGTAG, "RootView : Width = " + rw + " HEIGHT= " + rh + " dire " + rootView.getLayoutDirection());
Log.w("GenericAdPlugin", "show banner, overlap:" + GenericAdPlugin.this.overlap + ", position: " + argPos);
if (GenericAdPlugin.this.overlap) {
int x = GenericAdPlugin.this.posX;
int y = GenericAdPlugin.this.posY;
Log.d(LOGTAG, "run: params X " + x + " y " + y);
int ww = mainView.getWidth();
int wh = mainView.getHeight();
Log.d(LOGTAG, "run: ww = " + ww + " wh: " + wh);
if (GenericAdPlugin.this.overlapLayout == null) {
Toast.makeText(activity, "overlap if", Toast.LENGTH_LONG).show();
GenericAdPlugin.this.overlapLayout = new RelativeLayout(activity);
RelativeLayout.LayoutParams params = new LayoutParams(-1, -1);
// params.leftMargin = rw-500;
rootView.addView(GenericAdPlugin.this.overlapLayout, params);
}
GenericAdPlugin.this.overlapLayout.addView(GenericAdPlugin.this.adView);
GenericAdPlugin.this.overlapLayout.setGravity(RelativeLayout.ALIGN_PARENT_END|RelativeLayout.CENTER_IN_PARENT);
GenericAdPlugin.this.parentView = GenericAdPlugin.this.overlapLayout;
}
GenericAdPlugin.this.parentView.bringToFront();
GenericAdPlugin.this.parentView.requestLayout();
GenericAdPlugin.this.adView.setVisibility(0);
GenericAdPlugin.this.bannerVisible = true;
GenericAdPlugin.this.__resumeAdView(GenericAdPlugin.this.adView);
mainView.requestFocus();
}
}
});
}
}
活动的屏幕截图:
我想在屏幕的空白处显示广告。