如何在设置屏幕中为实时壁纸添加admob adview?

Jam*_*mes 8 layout android admob

我看过Mario动态壁纸在设置屏幕上使用了admob广告,但我自己无法做到.如果我将adview放入settings.xml中,就像使用普通布局一样,我会得到一个类转换异常.

这是马里奥动态壁纸的截图,以说明我正在尝试做什么.

示例截图

Bri*_*ian 20

这是一个更简单的解决方案:创建一个显示单个广告的新首选项类型.然后,您可以在首选项的xml定义中包含该首选项类型,以显示一个或多个广告.

自定义偏好类:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

AdmobPreference的XML布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

然后你只需在你的首选项xml定义中添加这样的东西:

<?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>
Run Code Online (Sandbox Code Playgroud)