布局未在所有设备上正确显示

时间:2019-03-26 11:58:44

标签: android cordova

我创建了动态布局,可在横向屏幕的右侧显示广告。广告布局在某些设备上正确显示在屏幕上,而在某些设备上未正确显示。

我为广告版式设置了页边距。

public void showBanner(final int argPos, final int argX, final int argY) {

    Log.d("GenericAdPlugin", "showBanner2 " + 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() {
            @SuppressLint("WrongConstant")
            public void run() {
                View mainView = GenericAdPlugin.this.getView();
                if (mainView == null) {
                    Log.e("GenericAdPlugin", "Error: could not get main view");
                } else {
                    Log.d("GenericAdPlugin", "webview class: " + mainView.getClass());
                    int bw = GenericAdPlugin.this.__getAdViewWidth(GenericAdPlugin.this.adView);
                    int bh = GenericAdPlugin.this.__getAdViewHeight(GenericAdPlugin.this.adView);
                    Log.d(LOGTAG, "run: bw = " + bw + " bh: " + bh);
                    Toast.makeText(getActivity(), "bw " + bw + "::bh " + bh, Toast.LENGTH_SHORT).show();
                    Log.d("GenericAdPlugin", String.format("show banner1: (%d x %d)", bw, bh));
                    ViewGroup rootView = (ViewGroup) mainView.getRootView();
                    int rw = rootView.getWidth();
                    int rh = rootView.getHeight();
                    Log.d(LOGTAG, "run: rw = " + rw + " rh: " + rh);
                    Log.w("GenericAdPlugin", "show banner, overlap:" + GenericAdPlugin.this.overlap + ", position: " + argPos);
                    Toast.makeText(activity, "overlap if", Toast.LENGTH_LONG).show();
                    int x = GenericAdPlugin.this.posX;
                    int y = GenericAdPlugin.this.posY;

                    int ww = mainView.getWidth();
                    int wh = mainView.getHeight();
                    Log.d(LOGTAG, "run: ww = " + ww + " wh: " + wh);
                    x = argX;
                    y = argY;

                    int hx = ww - bh;
                    int hy = wh / 2;
                    Log.d(LOGTAG, "run: params HX " + hx + " : " + hy);

                    Resources resources = activity.getResources();
                    int btmSize = 0;
                    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
                    if (resourceId > 0) {
                        btmSize = resources.getDimensionPixelSize(resourceId);
                        Log.d(LOGTAG, "run: Resource " + resources.getDimensionPixelSize(resourceId));
                    }
                    Log.d(LOGTAG, "run: btmSize " + btmSize + " MI " + (rw - btmSize));
                    LayoutParams params = new LayoutParams(bw, bh);

                    int marginend, marginstart, margintop, marginbottom;
                    final float scale = activity.getResources().getDisplayMetrics().density;
                    Log.d(LOGTAG, "run: scale " + scale);
                    margintop = (int) ((wh * 51.62) / 100);
                    marginbottom = (int) ((wh * 7.37) / 100);
                    if (isNavigationBarAvailable()) {
                        marginend = (int) (((rw * 35.15) / 100));
                        marginstart = (int) (((rw * 68.35) / 100));
                    } else {
                        marginend = (int) (((rw * 35.15) / 100) + btmSize);
                        marginstart = (int) (((rw * 68.35) / 100) + btmSize);
                    }
                    Log.d(LOGTAG, "run: MEnd " + marginend * scale + 0.5f);
                    Log.d(LOGTAG, "run: MStr " + marginstart * scale + 0.5f);
                    Log.d(LOGTAG, "run: MTop " + margintop * scale + 0.5f);
                    Log.d(LOGTAG, "run: MBtm " + marginbottom * scale + 0.5f);
                    Log.d(LOGTAG, "run: isNavigate " + isNavigationBarAvailable());
                    params.leftMargin = marginstart;
                    params.rightMargin = marginend;
                    params.topMargin = margintop;
                    params.bottomMargin = marginbottom;

                    Log.d(LOGTAG, "run: X = " + x + " Y: " + y);
                    if (GenericAdPlugin.this.overlapLayout == null) {
                        Toast.makeText(activity, "overlap if if", Toast.LENGTH_LONG).show();
                        GenericAdPlugin.this.overlapLayout = new RelativeLayout(activity);
                        rootView.addView(GenericAdPlugin.this.overlapLayout, new LayoutParams(-1, -1));
                    }
                    GenericAdPlugin.this.overlapLayout.addView(GenericAdPlugin.this.adView, params);
                    GenericAdPlugin.this.parentView = GenericAdPlugin.this.overlapLayout;

                    GenericAdPlugin.this.parentView.bringToFront();
                    GenericAdPlugin.this.parentView.requestLayout();
                    Log.d(LOGTAG, "run:AdView Width " + GenericAdPlugin.this.adView.getWidth() + " : " + GenericAdPlugin.this.adView.getHeight());
                    GenericAdPlugin.this.adView.setVisibility(0);
                    GenericAdPlugin.this.bannerVisible = true;
                    GenericAdPlugin.this.__resumeAdView(GenericAdPlugin.this.adView);
                    mainView.requestFocus();
                }
            }
        });
    }
}

我在这里显示我的代码

我想在所有设备上正确显示。

First ScreenShot In Pixel and Its Correct

Second ScreenShot In Wrong Position

0 个答案:

没有答案