我见过的马里奥动态壁纸使用AdMob的广告在设置画面,但我一直没能做到这一点我自己。如果我把AD浏览报入的settings.xml就像你要做一个正常的布局我得到一个类转换异常。
I've seen the Mario live wallpaper uses admob ads in the settings screen, but I haven't been able to do it myself. If I put the adview into the settings.xml like you would with a normal layout I get a class cast exception.
下面是马里奥的截图活的壁纸来说明我想要做的事情。
Here's a screenshot of the mario live wallpaper to illustrate what I'm trying to do.
下面是一个简单的解决方案:创建显示一个广告一个新的preference类型。然后,您可以包括在你的preferences XML定义了preference类型以显示一个或多个广告。
Here's a simpler solution: Create a new preference type that displays a single ad. You can then include that preference type in the xml definition for your preferences to display one or more ads.
自定义preference类:
Custom Preference Class:
public class AdmobPreference extends Preference
{
public AdmobPreference(Context context) {
super(context, null);
}
public AdmobPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected View onCreateView(ViewGroup parent) {
//override here to return the admob ad instead of a regular preference display
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.admob_preference, null);
}
}
XML布局Admob的preference:
XML Layout for AdmobPreference:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/<YOUR PACKAGE>"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<com.google.ads.AdView android:id="@+id/ad" android:layout_width="fill_parent"
android:layout_height="wrap_content" myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC" />
</LinearLayout>
然后你只需要添加像这样到preferences XML定义:
And then you just add something like this into your preferences xml definition:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:orderingFromXml="true">
<YOUR PACKAGE NAME.AdmobPreference android:key="ad" />
... other preferences ...
</PreferenceScreen>