【问题标题】:admob android helpadmob 安卓帮助
【发布时间】:2011-08-15 22:08:08
【问题描述】:

我在将 admob 正确放入布局时遇到问题。不管我怎么说,我似乎根本看不到广告。这是 2 种布局...

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000">
<ListView
android:id="@+id/android:list"
android:background="#0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dip"/>
</LinearLayout>

layout2.xml

<?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="?android:attr/listPreferredItemHeight"
android:padding="10dip"
android:background="#0000">
<TableLayout
android:layout_width="fill_parent"
 android:layout_weight="1"
 android:layout_height="fill_parent"
android:stretchColumns="*"
android:background="#0000">
<TableRow>
<TextView
android:id="@+id/description"
android:padding="2dip"
android:textColor="#ffff"
android:background="#0000"/>
</TableRow>
</TableLayout>
</LinearLayout>

admob java 的代码是 android:id="@+id/linearLayout" 已经在主布局中。问题是它不显示,但如果我创建一个新项目,除了在线布局的 admob java 部分之外什么都没有,我可以看到广告。

是的,我在主目录中添加了正确的代码

<activity android:name="com.google.ads.AdActivity" 
android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

我也被告知要使用相对布局,但这也不起作用,除非我做错了。希望有人能帮忙!

更新1: 这里是@+id/linearLayout 用于admob的java代码

    // Lookup R.layout.main 
    LinearLayout layout = 
   (LinearLayout)findViewById(R.id.linearLayout); 
    // Create the adView 
    // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID 
    AdView adView = new AdView(this, AdSize.BANNER, "a14e458091154df"); 
    // Add the adView to it 
    layout.addView(adView); 
    // Initiate a generic request to load it with an ad 
    AdRequest request = new AdRequest(); 
    request.setTesting(true); 
    adView.loadAd(request); 

【问题讨论】:

  • 是否有一些 Java 代码可以添加 adView?事实上,我看不到任何与 admob 相关的内容......
  • 是的,linearLayout 指向 java 代码中的 admob。这是我在 admob 网站上找到的唯一有效示例,所以我坚持使用它。一切似乎都在其中发挥作用。这是java中的代码......
  • javacode 在第一篇文章的 update1 中。

标签: android android-layout admob android-linearlayout android-relativelayout


【解决方案1】:

在您的 main.xml 中,按照下面的示例添加一个 AdView,也不要忘记添加 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 到主要的 LinearLayout 定义,并将 MY_AD_UNIT_ID 替换为您的 adMob 发布商 ID

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000">
  <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="true"/>
<ListView
android:id="@+id/android:list"
android:background="#0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dip"/>
</LinearLayout>

还可以在this page 上查看更多示例。

【讨论】:

  • 我逼近了。 javacode 在第一篇文章的 update1 中。
  • 请发布 LogCat 堆栈跟踪,应该有关于崩溃原因的信息...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多