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

问题描述

我有一个布局和属性:

ads:adSize = LARGE_BANNER

ads:adSize="LARGE_BANNER"

我想要放入
ads:adSize = @ dim ...昏暗的名字

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

但它给我错误
例如:FULL_BANNER
表示不存在完整横幅之类的东西,并且无法编译。

but it gives me errorfor example: FULL_BANNERit 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格式定义adview并引用中的值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大小:&lt; item&gt;需要'type'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:24