【问题标题】:Attributes/tags from com.google.ads namespace in styles.xml来自styles.xml 中com.google.ads 命名空间的属性/标签
【发布时间】:2013-04-14 23:18:13
【问题描述】:

我是 Ads 新手,并且能够在我的布局中使用以下代码在我的 Android 应用中成功添加我的第一个广告。

mylayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

<com.google.ads.AdView 
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adUnitId="xxx"
    ads:adSize="BANNER"
    ads:loadAdOnCreate="true"/>

由于我的应用中有多个布局,我将常见的 AdView 属性(例如 adSize、adUnitId 等)移动到了 styles.xml 以减少代码大小。但是,在此更改之后,我遇到了类似的错误 error: Error: No resource found that matches the given name: attr 'ads:adUnitId'. error: Error: No resource found that matches the given name: attr 'ads:adSize'. error: Error: No resource found that matches the given name: attr 'ads:loadAdOnCreate'.

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" >

    <style name="MyAdView">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="ads:adSize">BANNER</item>
        <item name="ads:adUnitId">xxx</item>
        <item name="ads:loadAdOnCreate">true</item>
    </style>

</resources>

android:xxx 标签没有问题,问题似乎只出在 ads:xxx 上。为了使用 com.google.ads 命名空间,我还应该做些什么。 由于当我在布局中有代码时一切正常,我想我拥有在我的应用程序中添加广告所需的一切(AdMob SDK、构建路径中的 admob 库以及订单和导出等)。 请建议。提前致谢。

【问题讨论】:

    标签: android admob


    【解决方案1】:

    我认为您只能将样式资源用于与样式相关的属性,如果您想分解 id,您应该使用字符串资源,例如

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="admob_id">XXX</string>
    </resources>
    

    【讨论】:

    • 感谢@gipi 的回复。我看到 ads: 命名空间的所有属性都有类似的错误,包括我认为是样式属性的 adSize。如果我错了,请纠正我。
    • 我认为由于库的 jar 不包含具有您无法引用的资源的 XML 文件:您没有 ads.R 命名空间可用。
    • 明确一点,ads:xxx 在布局 xml 中工作正常。仅当我在 styles.xml 中使用它们时才会看到错误。
    • 我知道,我自己试过代码; com.google.ads.AdView 包含在 jar 中,并且由于不是正确的 Android 库,因此您无法在 styles.xml 中访问或引用其命名空间。我想被反驳
    • 再次感谢@gipi。我接受这个答案并继续使用广告布局文件,直到找到更好的方法来处理这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多