Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

android - How do I put an admob adview in the settings screen for a live wallpaper?

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.

example screenshot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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 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>

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...