1

我是 Android 开发的新手,我正在尝试学习如何将 Admob 添加到现有的应用程序中。使用 Admob SDK 说明,我已经能够在我尝试过的几个应用程序上成功地做到这一点。我有一个,但是对于我的生活,我无法像它应该那样进入屏幕底部。我知道它在 main.xml 中的某些内容,但我无法弄清楚。

这是我现在将广告块放在屏幕中央的代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="@drawable/bg">

<ImageView android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:src="@drawable/eight_ball"
    android:layout_gravity="center_vertical" 
    android:layout_margin="10px" />
    <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:gravity="center">
        <TextView android:id="@+id/MessageTextView" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:background="@drawable/triangle" 
            android:text="@string/shake_me_caption" 
            android:focusable="false" 
            android:gravity="center_vertical|center"
                            android:layout_marginTop="14dip">
        </TextView>
</LinearLayout>
</FrameLayout>

我的主要活动java文件中处理这个的部分:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initialization();
    // Adding AdMob banner
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID); 
    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
    layout.addView(adView); 
    adView.loadAd(new AdRequest());
}

正如我所说,我很确定它的 main.xml 需要工作(可能也对主要活动中的代码进行了一些调整),但无论我尝试了什么,广告块要么留在中心,要么完全消失。

提前致谢。

4

1 回答 1

1
<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:adUnitId=""
        ads:adSize="BANNER"
        ads:loadAdOnCreate="true"/>

将此代码放在您的 xml 文件中您想要放置的任何位置。您不需要编写您在 java 文件中编写的代码。

只需将此代码插入 xml。就这样。干杯。

于 2013-06-25T02:17:54.860 回答