android - Admob 自适应横幅广告实施
问题描述
我正在用新的自适应横幅广告替换我当前的横幅广告。自适应广告尺寸是在捕获宽度尺寸后计算的。我将 adview 的 framelayout 放在布局的底部。
<Relativelayout>
..
//// linear layout above ad_view_container
<FrameLayout
android:id="@+id/ad_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true" />
</Relativelayout>
但是现在的问题是,首先加载整个页面,然后在 1 -2 秒后,整个布局向上移动,并且显示了 adative 横幅广告。
那么这不违反admob政策吗?这种情况应该如何处理,我应该为自适应广告设置高度,这样布局就不会发生上移?搜索了很多,但没有找到答案。提前致谢。
解决方案
如果您的唯一意图是询问这是否违反政策,我不确定。
但是为了防止布局在一段时间后发生变化,我使用了以下实现。当您像我一样有更复杂的布局组合时,它也会有所帮助。我使用ConstraintLayout
在纵向模式下将我的屏幕分成两部分的 a。横幅位于这两个部分之一中,它们之间的比例不是固定的,而是取决于某些逻辑。因此,此实现也适用于此要求,因为它onMeasure
会根据可用宽度覆盖以确定最佳尺寸。
public class AdBanner extends FrameLayout
{
private AdView mAdView;
private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
public AdBanner(Context context)
{
super(context);
}
public AdBanner(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public AdBanner(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (!isShowBanner())
{
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST));
return;
}
int width = MeasureSpec.getSize(widthMeasureSpec);
if (width <= 0)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// That's where we determine the most accurate banner format.
AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(getContext(), getDpWidth(width));
int height = adSize.getHeightInPixels(getContext());
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.UNSPECIFIED)
{
height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
}
setMeasuredDimension(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.getMode(heightMeasureSpec)));
}
protected int getDpWidth(int width)
{
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
display.getMetrics(mDisplayMetrics);
return (int) (width / mDisplayMetrics.density);
}
protected boolean isShowBanner()
{
// Do your checks here, like whether the user payed for ad removement.
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
if (!isShowBanner())
{
return;
}
int width = r - l;
if (width <= 0)
{
return;
}
AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(getContext(), getDpWidth(width));
// Prevent the ad from beeing added with each layout cicle,
// by checking, whether or not available size actually changed the format of the banner
if (mAdView == null || !adSize.equals(mAdView.getAdSize()))
{
removeAllViews();
mAdView = new AdView(getContext());
mAdView.setAdSize(adSize);
((GameApplication) getContext().getApplicationContext()).androidInjector().getAdService().loadBannerAd(getRootActivity(this), mAdView);
this.addView(mAdView);
}
mAdView.layout(0, 0, width, b - t);
}
}
推荐阅读
- python - 如何将csv文件与熊猫连接起来
- javascript - JS中`:`的意义是什么
- c# - 将 [columnnr] 中的 sql 数据拉到字符串中以供以后检查
- java - 如何在 Activiti 进程中从网关调用 Java 方法
- dart - Dart 和 Flutter 中具有强类型属性的匿名对象
- ruby-on-rails - 纱线安装不断失败
- python - 如何计算这个递归函数?
- html - 如何下推 h1 标签或如何设置 h1 标签的位置,使其显示在我的导航栏下方
- javascript - 添加元素时存储在数组中的重复对象
- sql-server - SSIS 查找转换使用表或查询