简体   繁体   中英

Android Admob 4.3.1 how do I make the adView show on top of the screen?

I've been looking at similar questions but they are more on how to get the ads to show up with this new Admob version, I'm getting ads to show so thats not my problem, my problem is that ads show in the bottom of the screen so when the app expands the content on the screen the adview disappears since it goes under all the content. my solution is to place the adview on top of the screen and that way no matter how much content is on the screen the adview will be visible! I was able to accomplish this before when the adview was embedded into my xml, but following the admob tutorial you don't put it on the xml anymore. So how can I place the Ad on top of the mainLayout? here is the code I have been trying.

    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
    adView = new AdView(this, AdSize.BANNER, "my code");
    layout.addView(adView);
    AdRequest r = new AdRequest();
    adView.loadAd(r);
    // this is what I try to get it on top of the screen, but is not working
    adView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    adView.bringToFront();

this is all since I haven't done anything to the XML, is still showing the adview in the bottom, so how can I get it on the top part of the screen? Thanks

Your code is setting the gravity of the AdView, but the AdView is within a LinearLayout. Place the LinearLayout where you want it to appear.

Here is the documentation for how to add your banner in XML in 4.3.1.

If you still want to do it in code, try making your top level layout a RelativeLayout, and use this code:

adView = new AdView(this, AdSize.BANNER, "my code");
AdRequest r = new AdRequest();
adView.loadAd(r);
RelativeLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, params);

When you add the view, add it with an index. This works for me:

layout.addView(adView, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM