使AdMob标语尺寸适合屏幕尺寸 [英] Adapting AdMob banner size to screen size

查看:131
本文介绍了使AdMob标语尺寸适合屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很努力寻找一种解决方案,以使横幅广告的尺寸适合设备。我有一台8英寸的三星平板电脑和一台htc手机。

I'm really struggling to find a solution to adapt the size of the banner to the device. I have a Samsung tablet 8" and a htc phone.

有一个应用程序(收音机)恰好可以满足我的需求。

There is one app (radio) that is exactly doing what I need.

平板电脑:

电话:

平板电脑上的宽度

我读了 https://firebase.google.com/docs/admob/android/banner ,但我找不到正确的解决方案。

I read https://firebase.google.com/docs/admob/android/banner but I can't find the right solution.

我尝试过:

    AdView mAdView;
    mAdView = (AdView) getActivity().findViewById(R.id.bac_adView);
    mAdView.setVisibility(View.VISIBLE);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxxxxx")
            .build();
    mAdView.loadAd(adRequest);

,我收到错误消息:java.lang.IllegalStateException:广告大小只能设置一次AdView。

and I get the error: java.lang.IllegalStateException: The ad size can only be set once on AdView.

但我什至不确定这是否是正确的方法。

But I'm not even sure this is the right way of doing.

任何指向

推荐答案

将AdView添加到要显示横幅的布局文件中。
使用智能横幅尺寸属性。

Add AdView to your layout file in which you want to show banner. Use smart banner size attribute.

 <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/ad_id">

现在加载adView

Now load the adView

AdView mAdView;
    mAdView = (AdView) getActivity().findViewById(R.id.bac_adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxxxxx") 
            .build(); 
    mAdView.loadAd(adRequest);

现在您的父布局为RelativeLayout,然后在adview中使用alignparentbottom属性属性来修复布局底部的添加。

Now your parent layout is RelativeLayout then use alignparentbottom attribute attribute in adview to fix the add at bottom of layout.

android:layout_alignParentBottom="true"

这看起来会更干净。

这篇关于使AdMob标语尺寸适合屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆