有没有办法将 admob 广告添加到 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
一样,您可以指定自己的自定义布局,只要它包含ID为ListView
的android.R.id.list
即可。因此,创建您需要的包含 ListView
和 AdView
的任何 XML 布局文件,并将该布局用于 PreferenceActivity
。
丹·戴尔的答案是正确的。我想详细说明一下,只是通过示例来澄清。您可以使用这样的布局(在 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);
}
P.Melch 的答案有一些变化 Adpreference 类如下所示 (因为它不适用于最新的 google play 广告库):
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;
}
}
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent();
viewGroup.addView(new AdView(...));
我实施了建议的方法,但遇到了将横幅放在顶部和填充问题的问题。因此,我使用了不同的方法并有兴趣与您分享。我正在研究一项偏好活动,尽管它已被弃用,但由于某种原因我必须继续研究它,而不是更改为 androidx 偏好库。
以下是以下步骤:
获取偏好活动的父级。
LinearLayout root = (LinearLayout)
findViewById(android.R.id.list).getParent().getParent().getParent();
创建一个名为banner_ad.xml的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.gms.ads.AdView
android:id="@+id/adview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_id" />
</LinearLayout>
膨胀布局。
LinearLayout adViewLayout = (LinearLayout)
LayoutInflater.from(this).inflate(R.layout.banner_ad,root,false);
AdView adView = adViewLayout.findViewById(R.id.adview);
加载添加。
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(device_id)
.build();
adView.loadAd(adRequest);
添加布局视图作为根视图的子视图。就我而言,我在索引 1 处使用了下面的工具栏。
root.addView(adViewLayout,1);
创建带有广告横幅的 xml 文件。这可以是任何类型,例如 AdMob、AdColony 等。
将此布局添加到活动布局的顶部。
ListView lv = getListView();
ViewGroup viewGroup = (ViewGroup)lv.getParent(); // this is LinearLayout
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate( R.layout.ads, null );
viewGroup.addView( view, 0);
通过
findViewById
找到广告横幅并初始化它。