gpt4 book ai didi

java - 出现时android获取adMob横幅高度

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:43 25 4
gpt4 key购买 nike

我正在向我的应用程序成功添加 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 或您使用的任何横幅类型。

Add Sizes Get Height

关于java - 出现时android获取adMob横幅高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10433140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com