动态更改 admob 横幅大小

changing admob banner size dynamically(动态更改 admob 横幅大小)
本文介绍了动态更改 admob 横幅大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事情就是这样,您可能知道 Admob 有一个 AdSize.* 函数,您可以在其中放置 Banner 来显示横幅广告,而 AD_banner` 则用于平板电脑横幅,我想要做的是获取设备的屏幕尺寸,以便我可以将其放入我的 if 语句中,然后为正确的设备放置正确的横幅,我希望我足够清楚.所以任何人都可以告诉我如何获得设备的屏幕尺寸?感谢你//////////这是我到目前为止所做的事情

Here is the thing, as you might know Admob has a AdSize.* function, where u put Banner to show banner ads, and AD_banner` for tablet banners, what i want to do is take a screen size of a device so that i could throw it in my if statement and then put the right banner for right device, i hope i was clear enough.So anyone can tell me how can i get the screen size of device? Thank u ////////// Here's what i have done so far

     DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

       if (dm.density==DisplayMetrics.DENSITY_HIGH) {
        AdView adView = new AdView(this, AdSize.BANNER,s);
        LinearLayout layout = (LinearLayout)findViewById(R.id.admob);
        layout.addView(adView);
        adView.loadAd(new AdRequest());
       }
       if (dm.density==DisplayMetrics.DENSITY_DEFAULT || dm.density==DisplayMetrics.DENSITY_LOW ) {
             AdView adView = new AdView(this, AdSize.BANNER,s);
             LinearLayout layout = (LinearLayout)findViewById(R.id.admob);
             layout.addView(adView);
            adView.loadAd(new AdRequest());

        }
        if(dm.density==DisplayMetrics.DENSITY_MEDIUM)
        {
            AdView adView = new AdView(this, AdSize.IAB_BANNER,s);
            LinearLayout layout = (LinearLayout)findViewById(R.id.admob);
            layout.addView(adView);
            adView.loadAd(new AdRequest());

        }

推荐答案

如果您的布局是在 xml 中定义的,您可以为每个屏幕尺寸创建一个布局(layout-xlarge/mylayout.xml、layout-large/mylayout.xml, layout-normal/mylayout.xml 等...)

If your layout is define in a xml, you can create one layout per screen size (layout-xlarge/mylayout.xml, layout-large/mylayout.xml, layout-normal/mylayout.xml, etc...)

更多信息:http://developer.android.com/guide/practices/screen_support.html

不要看密度,因为 10.1 英寸平板电脑的密度中等,但分辨率为 480x850 的 4.3 英寸手机密度较高.改用屏幕尺寸(xlarge large normal small).

Don't look at density, because, a 10.1" tablet has a medium density, but a 4.3" phone with a 480x850 resolution will have a high density. Use screen size instead (xlarge large normal small).

如果您需要以编程方式执行此操作,您可以通过以下方式获取屏幕尺寸:

If you need to do it programatically, you can get the screen size with this :

Configuration config = activity.getResources().getConfiguration();
int screenlayout = config.screenLayout;

要进行比较,请使用 Configuration.SCREENLAYOUT_xxx .

and to compare, use Configuration.SCREENLAYOUT_xxx .

这篇关于动态更改 admob 横幅大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)