如何获取 AdView 横幅高度
How to get AdView banner height
我正在使用 Admob AdView(自适应横幅),我想获取横幅的高度。横幅定义如下:
private AdSize getAdSize() { //https://developers.google.com/admob/android/banner/adaptive
// Step 2 - Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float widthPixels = outMetrics.widthPixels;
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
// Step 3 - Get adaptive ad size and return for setting on the ad view.
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
mAdView = new AdView(getContext());
mAdView.setAdUnitId("00000000000000000000");
AdSize adSize = new AdSize(getAdSize().getWidth(), (int) ((getAdSize().getHeight()/3)*2.5));//I want the banner to be a bit smaller than returned.
mAdView.setAdSize(adSize);
List<String> testDeviceIds = Arrays.asList("00000000000000");
RequestConfiguration configuration =
new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build();
MobileAds.setRequestConfiguration(configuration);
//insert adView to wrap_content container
con.addView(mAdView);
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
但问题是:
这个表达式: (int) ((getAdSize().getHeight()/3)*2.5)
returns 52 在我的例子中。 但这不是横幅的实际尺寸。(他更大)。
我试图在点击时打印横幅的高度,结果他实际上是 136px
.
是setAdSize()
错误吗? (或者可能是因为广告分辨率)?
但是,如何在加载横幅之前获得 real 高度? (无需等待像 getViewTreeObserver().addOnGlobalLayoutListener()
这样的横幅加载,顺便说一句,它也不起作用)。
我尝试使用:mAdview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
和 mAdView.getMeasuredHeight()
但它 returns 0.
并且我在 getViewTreeObserver().addOnGlobalLayoutListener()
和 onAdLoaded()
内尝试了 getHeight()
(即使我想在加载前知道高度)但它仍然是 0.
总结:
我有 2 个问题:
- 我可以看到横幅的高度不是从
(int) ((getAdSize().getHeight()/3)*2.5)
返回的 52
。那么为什么 setAdSize()
是假值? (以及如何避免)
- 我怎样才能得到显示的真实高度?
提前致谢。
AdSize (int width, int height)的构造函数:
width: The width of the ad in density-independent pixels.
height: The height of the ad in density-independent pixels.
最后我找到了修复 getViewTreeObserver().addOnGlobalLayoutListener()
方式的方法,方法是在广告加载后调用它:
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
//mAdView.getHeight() returns 0 since the ad UI didn't load
mAdView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mAdView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//mAdView.getHeight() works here
}
});
}
});
正如 Morrison Chang 所说,AdSize
构造函数的 width/height
值应该在 dp
中指定,而不是在 px
中指定:
width: The width of the ad in density-independent pixels.
height: The
height of the ad in density-independent pixels.
我正在使用 Admob AdView(自适应横幅),我想获取横幅的高度。横幅定义如下:
private AdSize getAdSize() { //https://developers.google.com/admob/android/banner/adaptive
// Step 2 - Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float widthPixels = outMetrics.widthPixels;
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
// Step 3 - Get adaptive ad size and return for setting on the ad view.
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
mAdView = new AdView(getContext());
mAdView.setAdUnitId("00000000000000000000");
AdSize adSize = new AdSize(getAdSize().getWidth(), (int) ((getAdSize().getHeight()/3)*2.5));//I want the banner to be a bit smaller than returned.
mAdView.setAdSize(adSize);
List<String> testDeviceIds = Arrays.asList("00000000000000");
RequestConfiguration configuration =
new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build();
MobileAds.setRequestConfiguration(configuration);
//insert adView to wrap_content container
con.addView(mAdView);
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
但问题是:
这个表达式: (int) ((getAdSize().getHeight()/3)*2.5)
returns 52 在我的例子中。 但这不是横幅的实际尺寸。(他更大)。
我试图在点击时打印横幅的高度,结果他实际上是 136px
.
是setAdSize()
错误吗? (或者可能是因为广告分辨率)?
但是,如何在加载横幅之前获得 real 高度? (无需等待像 getViewTreeObserver().addOnGlobalLayoutListener()
这样的横幅加载,顺便说一句,它也不起作用)。
我尝试使用:mAdview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
和 mAdView.getMeasuredHeight()
但它 returns 0.
并且我在 getViewTreeObserver().addOnGlobalLayoutListener()
和 onAdLoaded()
内尝试了 getHeight()
(即使我想在加载前知道高度)但它仍然是 0.
总结:
我有 2 个问题:
- 我可以看到横幅的高度不是从
(int) ((getAdSize().getHeight()/3)*2.5)
返回的52
。那么为什么setAdSize()
是假值? (以及如何避免) - 我怎样才能得到显示的真实高度?
提前致谢。
AdSize (int width, int height)的构造函数:
width: The width of the ad in density-independent pixels.
height: The height of the ad in density-independent pixels.
最后我找到了修复 getViewTreeObserver().addOnGlobalLayoutListener()
方式的方法,方法是在广告加载后调用它:
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
//mAdView.getHeight() returns 0 since the ad UI didn't load
mAdView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mAdView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//mAdView.getHeight() works here
}
});
}
});
正如 Morrison Chang 所说,AdSize
构造函数的 width/height
值应该在 dp
中指定,而不是在 px
中指定:
width: The width of the ad in density-independent pixels.
height: The height of the ad in density-independent pixels.