我正在向我的应用程序成功添加 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();
}
最佳答案
您甚至可以在将任何类型的横幅添加到布局之前获取它的高度。
int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);
或
int heightPixels = AdSize.FULL_BANNER.getHeightInPixels(myContext);
或 DIP 的
int heightDP = AdSize.BANNER.getHeight();
因此,根据您的需要,您可以这样做:
/** 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();
}
只需将 AdSize.BANNER
更改为 AdSize.SMART_BANNER
或您使用的任何横幅类型。
关于java - 出现时android获取adMob横幅高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10433140/