动态改变admob横幅尺寸
事情是这样的,您可能知道 Admob 有一个 AdSize.*
功能,您可以在其中放置 Banner
来显示横幅广告,并使用 AD_banner` 来显示平板电脑横幅,这是我想要的要做的就是获取设备的屏幕尺寸,以便我可以将其放入 if 语句中,然后为正确的设备放置正确的横幅,我希望我足够清楚。所以任何人都可以告诉我如何获得屏幕尺寸设备? 感谢你 /////////////////// 这是我到目前为止所做的
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());
}
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最新的 AdMob SDK 包括 AdSize.SMART_BANNER,它可以为设备提供最佳尺寸匹配的广告。
我自己使用的是旧版本,不知道这个功能:D
The most recent AdMob SDK includes AdSize.SMART_BANNER which enables the best size matched ad for the device.
I was using an older version myself and didn't knew about this feature :D
如果布局是在 xml 中定义的,则可以为每个屏幕尺寸创建一个布局(layout-xlarge/mylayout.xml、layout-large/mylayout.xml、layout-normal/mylayout.xml 等...)
更多信息请参见此处:http://developer.android.com/guide/practices/screens_support.html
不要看密度,因为10.1英寸平板电脑具有中等密度,但分辨率为480x850的4.3英寸手机将具有高密度。请改用屏幕尺寸(xlarge 大正常小)。
如果您需要以编程方式执行此操作,则可以使用以下命令获取屏幕尺寸:
并使用 Configuration.SCREENLAYOUT_xxx 进行比较。
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...)
More info here : http://developer.android.com/guide/practices/screens_support.html
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 :
and to compare, use Configuration.SCREENLAYOUT_xxx .