好吧,我现在已经弄清楚了。有两种可能的方法,一种非常简单,另一种需要更多的工作。
最简单的方法
只要 adwhirl 布局没有任何内容可显示,它就保持不可见。所以你可以简单地创建一个FrameLayout
包含后台的后备视图和前面的 adwhirl 视图,类似于:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="53dp"
android:layout_gravity="center_horizontal"
>
<!-- fallback view -->
<TextView
android:id="@+id/ad_fallback"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="nothing to say..."
>
</FrameLayout>
然后,您可以在代码中将视图添加到布局中(其中parentView
是上面显示的膨胀布局):
final DisplayMetrics dm = activity.getResources().getDisplayMetrics();
final AdWhirlLayout adView = new AdWhirlLayout(activity, ADWHIRL_ID);
adView.setMaxWidth((int) (dm.density * 320));
adView.setMaxHeight((int) (dm.density * 53));
adView.setGravity(Gravity.CENTER);
parentView.addView(adView);
就是这样。
更复杂的方式
In GoodNews尽管我想要一种更复杂的方式:当 AdWhirl 忙于获取广告并且没有任何内容可填充内部广告横幅(作为应用程序中的资源提供,以便它甚至如果互联网不可用,则可以使用)应该显示。加载消息很简单,因为它可以像上面所示的那样实现,但是动态内部横幅有点棘手。
解决方案是 AdWhirl 提供的强大自定义事件,不幸的是,这些事件的文档记录很差。第一步是在 AdWhirl Web 界面中创建自定义事件:
- 调出“广告网络设置”
- 点击顶部的“广告自定义事件”按钮
- 输入任意名称(例如“fallback”)和函数名称(后者将直接映射到 Java 类中的方法名称)
- 启用该事件并为其分配 0%
- 现在调出“回填优先级”屏幕并将回退事件移至列表末尾
上面的配置可确保仅当 AdWhirl 无法展示任何真实广告时才会触发您的自定义事件。
现在您需要在代码中处理该事件。因此你需要一个实现的类AdWhirlLayout.AdWhirlInterface
并定义一个不带参数的公共方法,其名称等于自定义事件指定的函数名称。然后,此方法可以将您的特定视图注入 AdWhirl 布局中:
class AdWhirlEventHandler implements AdWhirlLayout.AdWhirlInterface {
private final AdWhirlLayout adView;
public AdWhirlEventHandler(AdWhirlLayout adView) {
this.adView = adView;
}
@Override
public void adWhirlGeneric() {
// nothing to be done: Generic notifications should also be
// configurable in the AdWhirl web interface, but I could't find them
}
/**
* Will be called by AdWhirl when our custom event with the function name
* "fallback" is fired. Called via reflection.
*/
public void fallback() {
try {
final View fallbackView =
... // inflate the view to be shown here
adView.pushSubView(fallbackView);
/*
* reset backfill chain and schedule next ad update
*/
adView.adWhirlManager.resetRollover();
adView.rotateThreadedDelayed();
} catch (MyExpectedException ex) {
/*
* forward to next option from the backfill list
*/
adView.rolloverThreaded();
}
}
}
现在您需要向以下位置注册您的事件处理程序AdWhirlLayout
像这样:
adView.setAdWhirlInterface(new AdWhirlEventHandler(adView));
就是这样。