布局顶部的 Admob 横幅

Admob Banner on Top of layout

我想在屏幕顶部显示横幅,但是有问题。在我的代码中(这不是我的代码,因为我正在学习过程中)我有两个选择。在屏幕底部的屏幕下方或上方显示横幅。

    public void showBanner(final boolean inLayout) {
    //banner ad
    if (BANNER_AD_UNIT_ID.length() > 0) {
        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(BANNER_AD_UNIT_ID);

        if (inLayout) {
            //make ad visible on bottom of screen over surface
            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
            adView.setLayoutParams(params1);
        } else {
            //make ad visible on bottom of screen under surface
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.BOTTOM;
            params.weight = 0;
            adView.setLayoutParams(params);

        }

如果我将 params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);BOTTOM 更改为 TOP,横幅将显示在屏幕上方。但是,如果我将 params.gravity = Gravity.BOTTOM;BOTTOM 更改为 TOP,广告仍然显示在底部 :(

谁能帮我解决这个问题?非常感谢!

布局代码:

// Start loading the ad in the background.
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            public void onAdLoaded() {
                View parent = (View) adView.getParent();
                if (parent != null) {
                    if (!(parent.equals(layout) || parent.equals(linear_layout))) {
                        if (inLayout)
                            layout.addView(adView);
                        else
                            linear_layout.addView(adView);
                        recalculateScreen();
                    }
                } else {
                    //add new banner ad to screen
                    if (inLayout)
                        layout.addView(adView);
                    else
                        linear_layout.addView(adView);
                    recalculateScreen();
                }
            }
        });
    }
}

//add relative layout to linear layout for banner ad mobility
    linear_layout = new LinearLayout(this);
    linear_layout.setOrientation(LinearLayout.VERTICAL);
    linear_layout.addView(layout);

    setContentView(linear_layout);
    holder = surface.getHolder();

首先,我认为这段代码总体上 View 是一个 RelativeLayout,它有一个 LinearLayout,其中包含一些其他 Views.. 第一个代码对齐你的将 addz 添加到 parent 的底部,第二个代码将其对齐到 LinearLayout 视图容器中其容器的底部 -(如果我说得通,idk)

但是您的初始编辑有效,因为它相对于 parent 对齐,而您的第二次编辑 re-define 是 addview 在其容器中的位置..所以要更改它 在else代码中(假设我的逻辑是对的)

   //make ad visible on bottom of screen under surface
   LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
   params.gravity = Gravity.BOTTOM;
   params.weight = 0;
   adView.setLayoutParams(params);
   // my addition starts here
   //inLayout is linearlayout, that was what i thought but no.
   ViewGroup parent = (ViewGroup) adView.getParent(); // your linear layout
   if(parent.getChildCount() >0){
         View viewToTake = parent.getChildAt(0); // taking the first child element
         parent.remove(parent.indexOfChild(adView)); // you get the index of the adview layout and remove it
         parent.addView(adView,0); // adding it at the first position
         parent.addView(viewToTake,parent.getChildCount());// adding the view we took out back into play, you can decide to add it as the
         // second element since it was your initial first with parent.addView(viewToTake,1);

编辑 1 复制粘贴并留下第一个..

adView.loadAd(adRequest);
    adView.setAdListener(new AdListener() {
        public void onAdLoaded() {
            View parent = (View) adView.getParent(); 
            if (parent != null) {
                if (!(parent.equals(layout) || parent.equals(linear_layout))) { 
                    if (inLayout)
                        layout.addView(adView); 
                    else
                        linear_layout.addView(adView,0); // adding it to linearLayout first element, that's what the zero does
                    recalculateScreen();
                }
            } else {
                //add new banner ad to screen
                if (inLayout)
                    layout.addView(adView);// same here
                else
                    linear_layout.addView(adView,0); //same here, goes to the top
                recalculateScreen();
            }
        }
    });
}

} 对于相对布局,你可以做对齐 parent stuff