how to display admob smartbanner exactly on the bottom of the screen while using andEngine CropResolutionPolicy

一笑奈何 提交于 2019-12-12 02:21:57

问题


So heres the deal. I am making a game in andEngine and I use CropResoltionpolicy (how it works: http://android.kul.is/2013/10/andengine-tutorial-dealing-with-screen-sizes.html). I have engine camera set as 480x800 in horizontal orientation. but the devices resolution is 1920x1080. In my game top and bottom parts of the screen are overflowing devices screen (see the previous link, how red rectangles are overflowing on left and right of the screen). So when I add banner that starts directly on top (aligned to the top of the main game activity layout) it is partially hiddden. I can easly move banner a bit lower just by using '.setY()' but that does not work properly on other devices with different screens. So what I'm looking for is a method to set the banner always on top or bottom of the devices screen, not game view/layout because they are not the same.

My code that I use to show ads:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final DisplayMetrics displayMetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);


    this.adView = new AdView(this);
    this.adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
    this.adView.setAdUnitId("myAdId");
    com.google.android.gms.ads.AdRequest adrequest = new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR).build();
    this.adView.setY(displayMetrics.heightPixels - this.adView.getHeight()); // here I set position of my banner, but it does not work properly

    this.adView.loadAd(adrequest);

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


    layout.addView(this.mRenderSurfaceView);

    layout.addView(this.adView, params);

}

My main_activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id ="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_gravity="center"
android:gravity="center"

tools:context="com.hsdev.salesrep.MainActivity" >

</RelativeLayout>

回答1:


Looks like answer is easy enough if you think enough:

final DisplayMetrics displayMetrics = new DisplayMetrics();
this.adView.setY(displayMetrics.heightPixels - (this.adView.getHeight() * (displayMetrics.heightPixels / 480) ) - 50);

where 480 is a height of camera in andEngine and 50 is a height of your banner.



来源:https://stackoverflow.com/questions/29332639/how-to-display-admob-smartbanner-exactly-on-the-bottom-of-the-screen-while-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!