2011-02-16 72 views

回答

27

您還可以做的是創建一個自定義首選項,可以輕鬆添加到任何首選項屏幕。

將一個名爲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> 
+0

這真的很酷....幫我解決了一個非常大的問題。 – dell116 2011-07-29 03:11:47

+0

不錯的作品,讓我開心 – 2012-03-07 22:17:33

15

是的,PreferenceActivity僅僅是一個子類的ListActivity,並與ListActivity,你可以,只要它包含的android.R.id.list ID的ListView指定自己的自定義佈局。因此,創建包含ListViewAdView所需的任何XML佈局文件,並將該佈局用於PreferenceActivity

+0

真棒夥計! Thanx – Sourabh 2013-09-12 18:14:34

13

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); 
    } 
+0

真棒Dude Thanx – Sourabh 2013-09-12 18:15:33

2
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent(); 
viewGroup.addView(new AdView(...)); 
0

有一些變化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; 
    } 
}