如何在动态壁纸的设置屏幕中放置 admob 广告视图? [英] How do I put an admob adview in the settings screen for a live wallpaper?
问题描述
我已经在设置屏幕中看到马里奥动态壁纸使用了 admob 广告,但我自己一直无法做到.如果我像使用普通布局一样将 adview 放入 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.
这是 mario 动态壁纸的屏幕截图,用于说明我正在尝试做什么.
Here's a screenshot of the mario live wallpaper to illustrate what I'm trying to do.
推荐答案
这里有一个更简单的解决方案:创建一个新的首选项类型来显示单个广告.然后,您可以在 xml 定义中包含该首选项类型,以便您的首选项显示一个或多个广告.
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.
自定义偏好类:
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);
}
}
AdmobPreference 的 XML 布局:
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>
然后您只需在首选项 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>
这篇关于如何在动态壁纸的设置屏幕中放置 admob 广告视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!