2011-11-27 86 views
0

我試圖在屏幕頂部顯示adMob廣告橫幅。但是,如下所示,它顯示在中間。任何人都知道我可以解決這個問題?Android adMob顯示在屏幕中間

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    ndroid:id="@+id/linearLayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:background="@drawable/main_bg" > 

    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:paddingTop="30dip" 
     android:layout_height="wrap_content" 
     android:gravity="center" > 

     <ImageButton 
      android:id="@+id/mnwvbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="15dip" 
      android:background="@drawable/mnwvicon" /> 

     <ImageButton 
      android:id="@+id/reportsbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/reportsicon" /> 

    </LinearLayout> 

    <ImageButton 
     android:id="@+id/mnwvshowbutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/showicon" 
     android:paddingTop="30dip" /> 

</LinearLayout> 

這是我添加AdView中的我的LinearLayout在我的代碼:

// Admob Banner *********** 
    layout = (LinearLayout)findViewById(R.id.linearLayout); 
    adView = new AdView(this, AdSize.BANNER, "a14ed285a63276d"); 
    layout.addView(adView); 
    adView.loadAd(new AdRequest()); 
    // ************************* 

這裏是我的屏幕上顯示的內容:

enter image description here

+0

簡短的回答:@ + ID/LinearLayout中在你的佈局XML使用兩次,儘量給內部彼此的名字,這應該解決你的問題。 – yorkw

+0

@yorkw對不起,這是一個複製粘貼錯誤...在我的代碼它實際上不是那樣的。我編輯了我的問題,並忘記從父LinearLayout中取出它。雖然謝謝! – comead

回答

1

我建議你使用relativ佈局。在下面的佈局,根據你的廣告佈局具有ID「adLayout」:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    ndroid:id="@+id/linearLayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/main_bg" > 

    <LinearLayout 
     android:id="@+id/adLayout" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" /> 

    <LinearLayout 
     android:layout_below="@id/adLayout" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:paddingTop="30dip" 
     android:layout_height="wrap_content"> 
    [...] 
+0

謝謝!我最終不得不這樣做,然後使用圖形佈局來設置圖標的適當位置。雖然謝謝! – comead

1

您要添加到的linearLayout是父元素;整個屏幕。嘗試添加一個子線性佈局(完全包含在其中的另一個線性佈局)並設置android:gravity =「bottom」。

這是一個沒有按鈕的例子。

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

    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" > 

</LinearLayout> 

或相對父(該類型包括只是基本定型家長填滿屏幕,子包的廣告。):

<RelativeLayout 
style="@style/FPFP"> 
<LinearLayout 
    android:id="@+id/ads" 
    style="@style/WCWC" 
    android:layout_alignParentBottom="true"/> 
</RelativeLayout> 
+0

我做了您所要求的更改並更改了我的問題。我的問題中的圖像是我現在得到的。 (我知道XML沒有android:gravity =「bottom」,但它給出了幾乎相同的結果。) – comead

+0

當我在過去完成此操作時,實際上已將父元素設置爲相對佈局和廣告容器與'android:layout_alignParentBottom =「true」'的線性佈局。我會在我的答案結尾處添加該示例代碼。 – jarvisteve