PreferenceActivity中的Android Admob广告

lac*_*cas 25 android admob

有没有办法将admob广告添加到PreferenceActivity?如何?

P.M*_*lch 29

您还可以创建一个可以轻松添加到任何首选项屏幕的自定义首选项.

将名为ad_layout.xml的布局文件添加到res/layout文件夹中,稍后由AdMob填充.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">  
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

像这样创建一个名为AdPreference的类:

package com.example.adpreference;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class AdPreference extends Preference {

    public AdPreference(Context context, AttributeSet attrs, int defStyle) {super    (context, attrs, defStyle);}
    public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
    public AdPreference(Context context) {super(context);}

    @Override
    protected View onCreateView(ViewGroup parent) {
        // this will create the linear layout defined in ads_layout.xml
        View view = super.onCreateView(parent);

        // the context is a PreferenceActivity
        Activity activity = (Activity)getContext();

        // Create the adView
        AdView adView = new AdView(activity, AdSize.BANNER, "<your add id>");

        ((LinearLayout)view).addView(adView);

        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        adView.loadAd(request);     

        return view;    
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,在首选项xml文件中,您可以添加添加任何您喜欢的位置(在顶部或任何其他首选项之间).

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    ...

    <com.example.adpreference.AdPreference android:layout="@layout/ad_layout"/>

    ...
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)


Dan*_*yer 15

是的,a PreferenceActivity只是一个子类,ListActivity并且,ListActivity您可以指定自己的自定义布局,只要它包含ListViewID为的android.R.id.list.因此,创建包含a ListView和a所需的任何XML布局文件,AdView并使用该布局PreferenceActivity.


Gle*_*ech 14

Dan Dyer的回答是正确的.我想详细说明一下,只是为了澄清一下.你可以使用这样的布局(在res/layout下称为config.xml).

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:myapp="http://schemas.android.com/apk/res/com.xxxx" android:layout_height="fill_parent"
                android:layout_width="fill_parent">

    <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

    <com.admob.android.ads.AdView
            android:id="@+id/ad"
            android:layout_alignParentBottom="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            myapp:backgroundColor="#000000"
            myapp:primaryTextColor="#FFFFFF"
            myapp:secondaryTextColor="#CCCCCC"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在扩展PreferenceActivity的Activity中,您可以在onCreate方法中编写类似的内容;

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.config);
  }
Run Code Online (Sandbox Code Playgroud)