簡體   English   中英

布局頂部的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); BOTTOMTOP ,橫幅顯示在屏幕上方。 但是如果我改變params.gravity = Gravity.BOTTOM; BOTTOMTOP ,廣告仍顯示在底部:(

有人可以幫我嗎? 非常感謝!

inLayout代碼:

// 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包含其他View 。第一個代碼將addz對齊到父對象的底部,第二個代碼將addz對齊到其容器的底部。 LinearLayout視圖容器-(如果我有道理,請輸入idk)

但是您的初始編輯之所以有效,是因為它相對於父級相對對齊,而您的第二次編輯則重新定義了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();
            }
        }
    });
}

對於相對的layou,您可以進行對齊父項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM