PreferenceActivity 中的 Android Admob 广告

问题描述 投票:0回答:8

有没有办法将 admob 广告添加到 PreferenceActivity 中?怎么办?

android admob
8个回答
30
投票

您还可以创建一个自定义首选项,可以轻松添加到任何首选项屏幕。

将名为 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>

16
投票

是的,

PreferenceActivity
只是
ListActivity
的子类,与
ListActivity
一样,您可以指定自己的自定义布局,只要它包含ID为
ListView
android.R.id.list
即可。因此,创建您需要的包含
ListView
AdView
的任何 XML 布局文件,并将该布局用于
PreferenceActivity


16
投票

丹·戴尔的答案是正确的。我想详细说明一下,只是通过示例来澄清。您可以使用这样的布局(在 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);
  }

3
投票

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;
    }
}

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

1
投票

您只需为广告制作单独的布局并将其添加到

preference_screen.xml
,如
PreferenceCategory
。 您可以在附图中看到清晰的图片layout

preferenceScreen


0
投票

我实施了建议的方法,但遇到了将横幅放在顶部和填充问题的问题。因此,我使用了不同的方法并有兴趣与您分享。我正在研究一项偏好活动,尽管它已被弃用,但由于某种原因我必须继续研究它,而不是更改为 androidx 偏好库。

以下是以下步骤:

  1. 获取偏好活动的父级。

    LinearLayout root = (LinearLayout) 
    findViewById(android.R.id.list).getParent().getParent().getParent();
    
  2. 创建一个名为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>
    
  3. 膨胀布局。

    LinearLayout adViewLayout = (LinearLayout) 
    LayoutInflater.from(this).inflate(R.layout.banner_ad,root,false);
    AdView adView = adViewLayout.findViewById(R.id.adview);
    
  4. 加载添加。

    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(device_id)
            .build();
    adView.loadAd(adRequest);
    
  5. 添加布局视图作为根视图的子视图。就我而言,我在索引 1 处使用了下面的工具栏。

    root.addView(adViewLayout,1);
    

0
投票

创建带有广告横幅的 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
找到广告横幅并初始化它。

© www.soinside.com 2019 - 2024. All rights reserved.