有沒有辦法將admob廣告添加到PreferenceActivity?如何?Adpreb廣告在PreferenceActivity
回答
您還可以做的是創建一個自定義首選項,可以輕鬆添加到任何首選項屏幕。
將一個名爲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>
創建一個名爲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;
}
}
現在在首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>
是的,PreferenceActivity
僅僅是一個子類的ListActivity
,並與ListActivity
,你可以,只要它包含的android.R.id.list
ID的ListView
指定自己的自定義佈局。因此,創建包含ListView
和AdView
所需的任何XML佈局文件,並將該佈局用於PreferenceActivity
。
真棒夥計! Thanx – Sourabh 2013-09-12 18:14:34
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>
在延伸PreferenceActivity的Activity中,你在onCreate方法中寫了這樣的東西;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config);
}
真棒Dude Thanx – Sourabh 2013-09-12 18:15:33
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent();
viewGroup.addView(new AdView(...));
有一些變化P.Melch答案 Adpreference類是像下面(因爲它不與谷歌最新的遊戲廣告庫工作):
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();
AdView adView = new AdView(getContext());
adView.setAdUnitId("<your ad id>");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
((LinearLayout)view).addView(adView);
return view;
}
}
- 1. 廣告在谷歌廣告管理系統(DoubleClick)廣告沒有出現在IE
- 2. 將廣告標題添加到具有標題的新蜂巢PreferenceActivity中
- 3. DFP廣告管理系統返回空廣告文件(廣告)
- 4. 廣告及在Thymeleaf
- 5. 廣告手機小尺寸廣告
- 6. 安裝廣告(廣告)服務器
- 7. Firebase廣告,僅測試廣告
- 8. 廣告控件「無廣告可用」
- 9. 限制廣告跟蹤和廣告sdks
- 10. PubCenter廣告沒有與廣告展示
- 11. 728x90廣告尺寸在https上顯示廣告
- 12. 在dfp廣告後添加一個標題(「廣告」)
- 13. AdMob廣告未在廣告橫幅中顯示
- 14. 無法顯示廣告Errorcode:0在廣告中android
- 15. 在xaml中傳遞廣告單元ID以渲染admob廣告
- 16. 如何在Google Cast上投放廣告(廣告)
- 17. iPhone,在亂舞廣告只是視頻廣告會
- 18. 谷歌廣告(廣告查看)在Android應用程序錯誤
- 19. MPMoviePlayer或AVPlayer在視頻中間播放廣告(廣告)iOS
- 20. 在廣告中使用BluetoothLEAdvertisementDataSection時無效的廣告負載
- 21. 偏好在PreferenceActivity
- 22. Haskell廣告包
- 23. 圖片廣告
- 24. LWJGL廣告牌
- 25. Ibeacons和廣告
- 26. ios廣告sdk?
- 27. Qt3d廣告牌
- 28. IOS:iAD廣告
- 29. TAPIT 「無廣告」
- 30. 廣告合併?
這真的很酷....幫我解決了一個非常大的問題。 – dell116 2011-07-29 03:11:47
不錯的作品,讓我開心 – 2012-03-07 22:17:33