android admob 尺寸错误:A 'type'<item> 属性是必需的 [英] android admob size on dimensions error: A 'type' attribute is required for <item>

查看:29
本文介绍了android admob 尺寸错误:A 'type'<item> 属性是必需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局和属性:

ads:adSize="LARGE_BANNER"

ads:adSize="LARGE_BANNER"

我想把它放在ads:adSize="@dim...昏暗的名字"

I want to put it with ads:adSize="@dim... dim name"

但它给了我错误例如:FULL_BANNER它说诸如完整横幅之类的东西不存在并且无法编译.

but it gives me error for example: FULL_BANNER it says something like full banner doesn't exist and it doesn't compile.

我现在在样式上尝试过,但它也不起作用:

I tried it now on styles but it doesn't work too:

<item name="ads:adSize">LEADERBOARD</item>

它说错误:

有什么想法吗?

推荐答案

您可以根据屏幕分辨率以编程方式动态加载广告的尺寸.

You can dynamically load the ad's size based on screen resolution, programatically.

在您的活动类 onCreate() 中:

In your activity's class onCreate():

        AdSize adSize = AdSize.SMART_BANNER;

        DisplayMetrics dm = getResources().getDisplayMetrics();

        double density = dm.density * 160;
        double x = Math.pow(dm.widthPixels / density, 2);
        double y = Math.pow(dm.heightPixels / density, 2);
        double screenInches = Math.sqrt(x + y);

        if (screenInches > 8) { // > 728 X 90
            adSize = AdSize.IAB_LEADERBOARD;
        } else if (screenInches > 6) { // > 468 X 60
            adSize = AdSize.IAB_BANNER;
        } else { // > 320 X 50
            adSize = AdSize.BANNER;
        }

        LinearLayout adContainer = (LinearLayout) findViewById(R.id.your_parent_layout);
        adView = new AdView(this, adsize, "xxxxxxxxxx");
        AdRequest adRequest = new AdRequest();
        adView.loadAd(adRequest);

        // Place the ad view.
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        adContainer.addView(adView, params);

如果您更喜欢以 xml 格式定义广告视图并引用 values-sw600、values-sw720.. 文件夹中的值,您可以在 dimens.xml<中定义宽度和高度/代码>:

If you prefer defining the adview in xml format and referencing the values from the values-sw600, values-sw720.. folders you can define the width and height in dimens.xml:

在 values/dimens.xml

  <resources>
        <!-- Default screen margins, per the Android Design guidelines. -->
        <dimen name="admob_width">320dp</dimen>
        <dimen name="admob_height">50dp</dimen>
    </resources>

然后,在您的布局中:

<com.google.ads.AdView 
    android:id="@+id/adView"
    android:layout_width="@dimen/admob_width"
    android:layout_height="@dimen/admob_height"
    ads:adUnitId="your_unit_id"
    ads:adSize="SMART_BANNER"
    ads:loadAdOnCreate="true"/>

这篇关于android admob 尺寸错误:A &amp;#39;type&amp;#39;&amp;lt;item&gt; 属性是必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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