如何将AdMob添加到活动中?

时间:2019-02-12 20:33:28

标签: android admob

我正在做一个新应用。一切都完成了,但我仍然需要在应用程序底部实现一个Admob标语。我以前从没做过,所以我想问问有人可以帮我吗?

我已经在YouTube上的教程中尝试了一些方法,但没有帮助。

这里有个例子https://i.stack.imgur.com/zIgWp.png

这是我的代码:

public class MainActivity extends AppCompatActivity {

    private ListView mListNotes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListNotes = (ListView) findViewById(R.id.main_listview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main_activity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_create: //run NoteActivity in new note mode
                startActivity(new Intent(this, NoteActivity.class));
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();

        //load saved notes into the listview
        //first, reset the listview
        mListNotes.setAdapter(null);
        ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());

        //sort notes from new to old
        Collections.sort(notes, new Comparator<Note>() {
            @Override
            public int compare(Note lhs, Note rhs) {
                if(lhs.getDateTime() < rhs.getDateTime()) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });

        if(notes != null && notes.size() > 0) { //check if we have any notes!
            final NoteAdapter na = new NoteAdapter(this, R.layout.view_note_item, notes);
            mListNotes.setAdapter(na);

            //set click listener for items in the list, by clicking each item the note should be loaded into NoteActivity
            mListNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //run the NoteActivity in view/edit mode
                    String fileName = ((Note) mListNotes.getItemAtPosition(position)).getDateTime()
                            + Utilities.FILE_EXTENSION;
                    Intent viewNoteIntent = new Intent(getApplicationContext(), NoteActivity.class);
                    viewNoteIntent.putExtra(Utilities.EXTRAS_NOTE_FILENAME, fileName);
                    startActivity(viewNoteIntent);
                }
            });
        } else { //remind user that we have no notes!
            Toast.makeText(getApplicationContext(), "No notes Found"
                    , Toast.LENGTH_SHORT).show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我认为AdMob是指横幅广告?如果我对您的理解正确,您是否想将横幅广告添加到ListView中,以便它们可以与列表中的“注释”一起滚动?

横幅广告通过AdView进行展示,实际上是View

因此,当返回的位置适合您的广告时,您需要在AdView方法中显示getView()而不是您的便笺视图。

通过RecyclerViewListView可以更有效地完成此操作,因为RecyclerView具有内置的实现,可以根据视图的类型显示不同的视图集。

但是,我必须警告您不要这样做。

请勿将AdMob广告放置在可滚动列表中。

如AdMob政策中所述:https://support.google.com/admob/answer/6275345?hl=en&ref_topic=2745287

  

此外,横幅广告不应随着用户滚动而移动,因为用户可能会尝试点击菜单,但最终却意外地点击了广告。此特定实施违反政策,我们保留禁用向您的应用投放广告的权利。

AdMob横幅广告不应由用户滚动。这样做将使您极有可能被禁止您的应用接收来自AdMob的未来广告。

如果这很严重,则有可能导致您的AdMob帐户也被关闭。

您在其他应用中看到的可滚动列表中的广告很可能从AdMob获取其广告,因此它们不会处于这种风险之中。

如果您仍然希望将横幅广告放在可滚动列表中,但又不想被禁止,则建议您查看AdMob以外的其他广告提供商。查看其他广告提供商的政策,查看是否禁止滚动广告。如果不是,请使用它们代替AdMob。

答案 1 :(得分:0)

以下是简单的xml代码,用于在ListView下面实现AdMob bannerListView。也许这对您的目的会有所帮助。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
    </ListView>
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true">

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/Id" />
</RelativeLayout>

位于列表中心的横幅不切实际。看起来好像涵盖了列表。