java - 在 ListView 中包含 AdMobs 广告

标签 java android admob

我想在我的应用程序中包含 AdMobs 广告,但我不确定如何添加它们。

我的主要 Activity 在我的 main.xml 上调用了 setContentView,但是我的 main.xml 只包含一个 ListView

<ListView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/listView"
  android:layout_width="match_parent" 
  android:layout_height="wrap_content"
  android:drawSelectorOnTop="true"
  />

将广告 View 添加到布局中,

<ListView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:myapp="http://schemas.android.com/apk/res/com.t3hh4xx0r.romcrawler"
  android:id="@+id/listView"
  android:layout_width="match_parent" 
  android:layout_height="wrap_content"
  android:drawSelectorOnTop="true" >

  <com.google.ads.AdView 
    android:id="@+id/ad"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    myapp:backgroundColor="#000000" 
    myapp:primaryTextColor="#FFFFFF" 
    myapp:secondaryTextColor="#CCCCCC" />
  </ListView>

当然会导致启动时强制关闭,因为 listView 不允许有子布局。

解决这个问题的最佳方法是什么?在 java 中创建广告 View ?如果是这样,您能否提供一些示例代码来说明我将如何创建它?

提前致谢!

最佳答案

创建如下布局:这样广告将显示在底部。通过将 weightSum 设置为 1,然后说 ListView 的权重为 1,它将占用创建广告后剩余的所有空间。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >

    <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#E4E9F5"
    android:cacheColorHint="#00000000" />

    <com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="your_adunitid_here"
    ads:loadAdOnCreate="true" />

</LinearLayout>

关于java - 在 ListView 中包含 AdMobs 广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734137/

相关文章:

java - 采取动态输入的一副纸牌

java - Java实现电脑围棋棋盘的简单方法

Android网络状态变化检测需要时间

android - 为什么 TextView 中的文本在回收器 View 中被截断?

android - 如何获得当前屏幕配置的最佳 admob 尺寸?

android - 我应该保密我的 AdMob 发布商 ID 吗?

java - 无法解决 bluej 中的对象错误

java - 读取 S3 文件时出现 "java.net.SocketException: Socket is closed"

android - 尝试持久存储 android.graphics.Path 值

react-native - Facebook 观众竞价不起作用 : bundle status is invalid