styles.xml 中 com.google.ads 命名空间中的属性/标签

3

我是新手,刚学习广告,使用以下代码在我的布局中成功地添加了第一个广告。

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、构建路径和顺序和导出等)。 请给予建议。提前致谢。

1个回答

1
我认为你只能将样式资源用于与样式相关的属性,如果你想要因式分解id,你应该使用字符串资源,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="admob_id">XXX</string>
</resources>

感谢您的回复,@gipi。我看到来自广告命名空间的所有属性都有类似的错误,包括adSize,我认为这是一个样式属性。如果我错了,请纠正我。 - MemoryLeak
жҲ‘и®Өдёәз”ұдәҺеә“зҡ„jarж–Ү件дёҚеҢ…еҗ«еёҰжңүиө„жәҗзҡ„XMLж–Ү件пјҢеӣ жӯӨжӮЁж— жі•еј•з”Ёе®ғ们пјҡжӮЁжІЎжңүеҸҜз”Ёзҡ„ads.Rе‘ҪеҗҚз©әй—ҙгҖӮ - gipi
只是为了明确,ads:xxx在布局xml中运行良好。我只在styles.xml中使用它们时才会看到错误。 - MemoryLeak
1
我知道,我已经尝试过这段代码了;com.google.ads.AdView 包含在 jar 中,但由于它不是一个合适的 Android 库,所以你无法在 styles.xml 中访问或引用其命名空间。我希望能被反驳。 - gipi
再次感谢 @gipi。我接受这个答案,并继续使用布局文件来展示广告,直到找到更好的处理方法。 - MemoryLeak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接