eclipse android在出现时获取adMob横幅高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10433140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android get adMob banner height when appears
提问by Jaume
I am adding an adMob banner to my app successfully. When banner appears I need to get its height in order to resize all layout elements. I am using the event onReceivedAd, that is properly fired. However, alturaBanner is = 0. Then, how to get its height? thank you.
我成功地向我的应用程序添加了一个 adMob 横幅。当横幅出现时,我需要获取其高度以调整所有布局元素的大小。我正在使用 onReceivedAd 事件,该事件已正确触发。但是alturaBanner是=0。那么,如何得到它的高度呢?谢谢你。
/** Called when an ad is received. */
@Override
public void onReceiveAd(Ad ad)
{
adView.setVisibility(View.VISIBLE);
int alturaBanner = adView.getHeight();
RelativeLayout.LayoutParams params1 = (android.widget.RelativeLayout.LayoutParams) browse2
.getLayoutParams();
params1.setMargins(0, alturaBanner, 0, 0);
Log.d(LOG_TAG, "onReceiveAd");
Toast.makeText(this, "onReceiveAd", Toast.LENGTH_SHORT).show();
}
采纳答案by android developer
getting the height of the view before it was prepared will always return you 0 . use the next code in order to get its correct size , no matter which device/screen you have:
在准备好之前获取视图的高度将始终返回 0 。使用下一个代码以获得正确的大小,无论您拥有哪种设备/屏幕:
private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable)
{
final ViewTreeObserver vto = view.getViewTreeObserver();
final OnPreDrawListener preDrawListener = new OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
runnable.run();
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.removeOnPreDrawListener(this);
return true;
}
};
vto.addOnPreDrawListener(preDrawListener);
}
inside the given runnable , you can query the real size of the view.
在给定的 runnable 中,您可以查询视图的实际大小。
alternatively , you can use addOnGlobalLayoutListener instead of addOnPreDrawListener if you wish.
或者,如果您愿意,您可以使用 addOnGlobalLayoutListener 而不是 addOnPreDrawListener。
another approach is to use onWindowFocusChanged (and check that hasFocus==true) , but that's not always the best way ( only use for simple views-creation, not for dynamic creations)
另一种方法是使用 onWindowFocusChanged (并检查 hasFocus==true),但这并不总是最好的方法(仅用于简单的视图创建,不适用于动态创建)
EDIT: Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126
编辑:替代 runJustBeforeBeingDrawn:https://stackoverflow.com/a/28136027/878126
回答by XdebugX
You can get the height of any type of banner before it is even added to the layout.
您甚至可以在将任何类型的横幅添加到布局之前获取其高度。
int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);
or
或者
int heightPixels = AdSize.FULL_BANNER.getHeightInPixels(myContext);
or for DIP's
或用于 DIP
int heightDP = AdSize.BANNER.getHeight();
So for your need, you could do this:
因此,根据您的需要,您可以这样做:
/** Called when an ad is received. */
@Override
public void onReceiveAd(Ad ad)
{
adView.setVisibility(View.VISIBLE);
int alturaBanner = AdSize.BANNER.getHeight(); // This gets the adsize, even if the view is not inflated.
RelativeLayout.LayoutParams params1 = (android.widget.RelativeLayout.LayoutParams) browse2
.getLayoutParams();
params1.setMargins(0, alturaBanner, 0, 0);
Log.d(LOG_TAG, "onReceiveAd");
Toast.makeText(this, "onReceiveAd", Toast.LENGTH_SHORT).show();
}
Just change AdSize.BANNER
to AdSize.SMART_BANNER
or whatever banner type your using.
只需更改AdSize.BANNER
为AdSize.SMART_BANNER
或任何您使用的横幅类型。
回答by Brian
I use the following method to get AdView's height:
我使用以下方法获取 AdView 的高度:
adView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = adView.getHeight();
if (height > 0) {
// now the height is gotten, you can do things you want
}
}
});
onGlobalLayout() is triggered when the global layout state or the visibility of views within the view tree changes.
onGlobalLayout() 当全局布局状态或视图树中视图的可见性发生变化时触发。
回答by Green goblin
Actually, you don't need to wait for appearance of adview to get adMob banner height if you are using smart banner type for showing banner ads.
实际上,如果您使用智能横幅类型来显示横幅广告,则无需等待 adview 出现即可获取 adMob 横幅高度。
Use Smart banner as it automatically decides the height of ad based on device size. Use full width of screen to show the ad.
使用智能横幅,因为它会根据设备尺寸自动决定广告的高度。使用整个屏幕宽度来展示广告。
From android developers site:
来自 android 开发者网站:
Smart Banners are ad units that will render screen-wide banner ads on any screen size across different devices in either orientation. Smart Banners help deal with increasing screen fragmentation across different devices by "smartly" detecting the width of the phone in its current orientation, and making the ad view that size.
Three ad heights (in dp, density-independent pixel) are available:
32 - used when the screen height of a device is less than 400
50 - used when the screen height of a device is between 400 and 720 90 - used when the screen height of a device is greater than 720
智能横幅广告单元将在任何屏幕尺寸的不同设备上以任一方向呈现全屏横幅广告。智能横幅通过“智能”检测手机当前方向的宽度,并使广告视图达到该尺寸,从而帮助解决不同设备上日益增加的屏幕碎片问题。
提供三种广告高度(以 dp 为单位,与密度无关的像素):
32 - 设备屏幕高度小于 400
时使用 50 - 设备屏幕高度在 400 到 720 之间时使用 90 - 设备屏幕高度大于 720 时使用
Now, get the height of AdView and adjust the margin of the layout where you wish to place the banner ad. Once the ad is loaded (by overriding on onAdLoaded
API), you know the height using below method:
现在,获取 AdView 的高度并调整您希望放置横幅广告的布局的边距。加载广告后(通过覆盖onAdLoaded
API),您可以使用以下方法知道高度:
public static int getAdViewHeightInDP(Activity activity) {
int adHeight = 0;
int screenHeightInDP = getScreenHeightInDP(activity);
if (screenHeightInDP < 400)
adHeight = 32;
else if (screenHeightInDP <= 720)
adHeight = 50;
else
adHeight = 90;
return adHeight;
}
public static int getScreenHeightInDP(Activity activity) {
DisplayMetrics displayMetrics = ((Context) activity).getResources().getDisplayMetrics();
float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;
return Math.round(screenHeightInDP);
}
回答by Florian
I had the same need to be able to display my own ads when AdMob fails to receive an ad or receives an empty ad (height=0).
当 AdMob 无法收到广告或收到空广告(高度 = 0)时,我同样需要能够展示我自己的广告。
I use the following code based on the fact that an AdView extends RelativeLayout:
我使用以下代码基于 AdView 扩展 RelativeLayout 的事实:
mAdMobView = new AdView(pActivity, AdSize.SMART_BANNER, Constants.ADMOB_AD_UNIT_ID);
mAdMobView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(final View pV, final int pLeft, final int pTop, final int pRight, final int pBottom, final int pOldLeft, final int pOldTop, final int pOldRight, final int pOldBottom) {
final float lAdHeight = mAdMobView.getHeight();
if (lAdHeight == 0) {
Debug.i(LOG_TAG, "mAdMobView.onLayoutChange(...) mAdMobView.height='" + lAdHeight + "'. AdMob returned an empty ad !");
// Show custom ads
} else {
Debug.d(LOG_TAG, "mAdMobView.onLayoutChange(...) mAdMobView.height='" + lAdHeight + "'");
// Make AdView visible
}
}
});
mAdMobView.setAdListener(new AdListener() {
@Override public void onReceiveAd(final Ad pAd) {
Debug.d(LOG_TAG, "onReceiveAd(...) AdMob ad received (mAdMobView.visibility='" + mAdMobView.getVisibility() + "').");
}
@Override public void onPresentScreen(final Ad pAd) {
Debug.d(LOG_TAG, "onPresentScreen(...)");
}
@Override public void onLeaveApplication(final Ad pAd) {
Debug.d(LOG_TAG, "onLeaveApplication(...)");
}
@Override public void onDismissScreen(final Ad pAd) {
Debug.d(LOG_TAG, "onDismissScreen(...)");
}
@Override
public void onFailedToReceiveAd(final Ad pAd, final ErrorCode pErrorCode) {
Debug.i(LOG_TAG, "onFailedToReceiveAd(...) AdMob ad error (" + pErrorCode + ").");
// Show custom ads
}
});
The code in 'onLayoutChange' is executed every time Admob receives a new ad.
每次 Admob 收到新广告时,都会执行“onLayoutChange”中的代码。
EDIT: My answer is not proper since this method was added with the API 11... I changed it for the use of onPreDraw() as explained in the previous answer.
编辑:我的答案不正确,因为此方法是与 API 11 一起添加的...我将其更改为使用 onPreDraw(),如上一个答案中所述。