2014-01-15 34 views
1

我在同一頁上有兩個廣告,但我想創建一個廣告。我有兩個admob廣告頂部和底部在android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background1" 
    tools:context=".Search"> 

    <RelativeLayout 
     android:id="@+id/RelativeLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <com.google.ads.AdView 
      android:id="@+id/adView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      ads:adSize="SMART_BANNER" 
      ads:adUnitId="MY - ID" 
      ads:loadAdOnCreate="true"/> 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:scrollingCache="false" 
      android:layout_above="@+id/adView"> 
     </ListView> 
    </RelativeLayout> 
</RelativeLayout> 

我加入了廣告這種方式,但我想我必須添加Java代碼此advertisement.However,如果我添加Java代碼我有兩個廣告的頂部和底部。 xml代碼提供底部廣告以顯示advertisement.xml代碼是否足以添加廣告?

Java代碼是在這裏:

RelativeLayout RelativeLayout1 = (RelativeLayout)findViewById(R.id.RelativeLayout1); 
AdView adView=new AdView(this,AdSize.SMART_BANNER,"MY - ID"); 
RelativeLayout1.addView(adView); 
AdRequest request = new AdRequest(); 
adView.loadAd(request); 
+0

你的問題沒有多大意義。你想在你的網頁上只有一個廣告?那麼爲什麼不刪除所有代碼/ xml指的是第二個。 – Doomsknight

+0

因爲我這樣做了,就像我說過的那樣xml代碼是否足以在我的頁面上添加一個廣告,還是必須編寫代碼xml和java?它們是添加廣告的不同方式嗎? ? firt的方式是編寫xml代碼,第二種方式是編寫java代碼? – RakeDevelop

+0

所以你想知道,如果你需要代碼和XML,或只是XML加載廣告。但也可以說,XML本身的作品。這不是回答你自己的問題嗎?我假設'ads:loadAdOnCreate =「true」'使它工作,並且不需要代碼。 – Doomsknight

回答

0

根據您說的意見,

是的,你只需要一個或另一個。

你不需要兩個

爲了解釋下面的代碼,我已經添加評論:

//Obtains your main layout. 
RelativeLayout RelativeLayout1 = (RelativeLayout)findViewById(R.id.RelativeLayout1); 
//Creates a new AdView 
AdView adView=new AdView(this,AdSize.SMART_BANNER,"MY - ID"); 
//Adds this new adView, to your page 
RelativeLayout1.addView(adView); 
//Gets a new ad request 
AdRequest request = new AdRequest(); 
//Tells the new adView to show the ad. 
adView.loadAd(request); 

從這一點,我們可以看到,它正在創造一個新的AdView,將它添加到頁面,然後在廣告加載。

因爲,只使用xml,Adview已經存在..所以沒有理由創建一個新的。並在xml中有ads:loadAdOnCreate="true",爲您加載廣告。

他們是實現同樣目標的兩種方式。

p.s.我更喜歡xml的做法。

+0

非常感謝。 – RakeDevelop

+0

@ user3139336不用擔心:) – Doomsknight

相關問題