Есть ли способ добавить объявление admob в PreferenceActivity? Как?
Объявление Android Admob в PreferenceActivity
Ответ 1
То, что вы также можете сделать, это создать пользовательскую настройку, которая может быть легко добавлена на любой экран настроек.
Добавьте файл макета с именем 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>
Ответ 2
Да, a PreferenceActivity
является всего лишь подклассом ListActivity
и, как и в случае с ListActivity
, вы можете указать свой собственный макет, если он содержит ListView
с идентификатором android.R.id.list
, Поэтому создайте любой XML файл макета, который вам нужен, ListView
и AdView
, и используйте этот макет для PreferenceActivity
.
Ответ 3
Ответ Дана Дайера правильный. Я хотел бы немного уточнить, просто для пояснения на примере. Вы можете использовать такой макет (называемый config.xml под res/layout).
<?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, вы пишете что-то подобное в методе onCreate;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config);
}
Ответ 4
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent();
viewGroup.addView(new AdView(...));
Ответ 5
Есть несколько изменений в ответе P.Melch. Класс Adpreference выглядит как (потому что он не работает с последней библиотекой рекламных объявлений Google):
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;
}
}