創建一個新類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;
}
}
在你的資源/ 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"/>
就是這樣。它會很好地工作。
回覆:您的附加信息:有可能廣告沒有出現,因爲他們只是沒有廣告來展示,而不是電話之間的差異。我一直在使用admob,只有約70-80%的請求實際上導致了廣告顯示(填充率%),否則廣告視圖就不會出現。不知道這是否有幫助:) – Jay