新しいクラス AdPreference.java を作成し、次のコードを含めます。
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_ADMOB_ID_HERE");
((LinearLayout)view).addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
adView.loadAd(request);
return view;
}
}
res/layout フォルダに ad_layout.xml というタイトルの新しい xml レイアウトを作成し、正確でなければなりません。次に、次のコードを含めます。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
次に、「設定」xml で、xmlns 行の直後にこれを追加します。
<com.yourpackagename.AdPreference android:layout="@layout/ad_layout"/>
AndroidManifest.xml でこれを前に追加します
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" android:value="false" />
これもマニフェストに追加します。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
以上です。それは完璧に動作します。