问题描述
我似乎找不到任何关于通过 XML 或 Java 为 adMob 广告设置关键字的可靠文档.我已经尝试了 this 线程中看到的方法,但在这两个方面都失败了.我在每种情况下的失败情况如下.
I can't seem to find any rock solid documentation on setting keywords for adMob ads via XML or Java. I've tried the methods seen in this thread, but have failed in both respects. My failures for each case are below.
当尝试使用 AdView 类中的方法 setKeywords()、setSearchQuery() 或 requestFreshAd() 时,Eclipse 似乎无法识别这些方法是 AdView 类的一部分.我可以不正确地实例化这个类吗?我的java代码如下:
When trying to use methods setKeywords(), setSearchQuery(), or requestFreshAd() from the AdView class, it's like Eclipse doesn't recognize that those methods are part of the AdView class. Could I be instantiating this class improperly? My java code is below:
import com.google.ads.AdRequest;
import com.google.ads.AdView;
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
如果我尝试调用上述方法(例如 adView.setKeywords() ),Eclipse 将无法识别这些方法.我做错了什么?
If I try to call the aforementioned methods, (eg. adView.setKeywords() ), Eclipse doesn't recognize the methods. What am I doing wrong?
当我尝试通过 XML 设置关键字时,代码似乎运行正常,但我没有收到任何有效的添加请求.日志返回无广告可显示"消息.在这种情况下,我不知道是我没有从我的关键字中获得结果,还是我没有正确设置关键字字符串.
When I attempt to set the keywords via XML, the code seems to run ok, but I don't get any valid requests for an add. The log returns with a "No ad to display" message. In this case, I don't know if I'm just not getting a result from my keywords or if I'm not settings the keyword string correctly.
以下描述了我如何通过 XML 设置关键字:
The following depicts how I'm setting the keywords via XML:
...在 attrs.xml 中:
...in attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.google.ads.AdView">
<attr name="adSize">
<enum name="BANNER" value="1"/>
<enum name="IAB_MRECT" value="2"/>
<enum name="IAB_BANNER" value="3"/>
<enum name="IAB_LEADERBOARD" value="4"/>
</attr>
<attr name="adUnitId" format="string"/>
<attr name="test" format="boolean"/>
<attr name="keywords" format="string"/>
</declare-styleable>
</resources>
在 layout.xml....
in layout.xml....
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
ads:adUnitId="blahblahblhal"
ads:adSize="BANNER"
ads:test="true"
ads:keywords="farts cheese stink pooper"/>
显然,这些不是我使用的关键字,但是我设置关键字的语法与我一直使用的相同.
Obviously, these are not the keywords I'm using, but the syntax by which I set the keywords is the same as I've been using.
如果没有广告可以展示,是因为我使用了太多关键字、语法不正确,以及如果我想使用词组而不是关键字怎么办?我该怎么做呢?
If there is no ad to display is it because I'm using too many keywords, improper syntax, and what if I want to use a phrase instead of a keyword? How would I go about doing that?
如果 AdMob 网站上有与此相关的文档,我深表歉意.但如果是这样,他们就很难找到了.
If there is documentation on the AdMob site pertaining to this I apologize. But if so, they made it pretty damn hard to find.
谢谢
推荐答案
setKeywords
是 AdRequest
类的方法,而不是 AdView
.
setKeywords
is a method of the AdRequest
class, not AdView
.
AdRequest request = new AdRequest();
Set<String> keywords = new HashSet<String>();
keywords.add("keyword1");
keywords.add("keyword2");
request.setKeywords(keywords);
adView.loadAd(request);
这篇关于Android for Admob 中的 XML 或 Java 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!