2013-03-01 129 views
1

我爲Admob編寫了代碼。這是我的layout.xml文件代碼:Admob橫幅不在屏幕的底部

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffff" 
android:id="@+id/relativeLayoutHomeParent" 
tools:context=".Home" xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"> 

<RelativeLayout 
    android:id="@+id/relativeLayoutHomeTopBar" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 

    <TextView 
     android:id="@+id/textView1" 
     android:text="Home" 
     style="@style/screen_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <ImageView 
     android:id="@+id/imageViewHomeSettings" 
     android:layout_width="22dp" 
     android:layout_height="22dp" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/set_settings" 
     android:layout_marginRight="10dp" /> 

</RelativeLayout> 

<View 
    android:id="@+id/view1" 
    android:layout_width="match_parent" 
    android:layout_height="5dp" 
    android:layout_below="@+id/relativeLayoutHomeTopBar" 
    android:background="@drawable/shadow" /> 

<ListView 
    android:id="@+id/listViewHome" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:divider="#BDBDBD" 
    android:dividerHeight="1dp" 
    android:layout_below="@+id/view1" > 
</ListView> 

<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" 
    app:adSize="BANNER" 
    app:adUnitId="a1512f50d8c3692" 
    app:loadAdOnCreate="true" 
    app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" > 
</com.google.ads.AdView> 

輸出:

Screen shot

我activity.java代碼:

package com.walletapp; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
    import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.ads.AdRequest; 
import com.google.ads.AdSize; 
import com.google.ads.AdView; 

public class Home extends Activity 
{ 
ListView listview_home; 
ImageView imageview_settings; 
SQLiteDatabase database; 
String database_name = "WalletAppDatabase"; 
private AdView adView; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    imageview_settings = (ImageView) findViewById(R.id.imageViewHomeSettings); 
    imageview_settings.setClickable(true); 
    imageview_settings.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) 
     {    
      startActivity(new Intent(Home.this, Settings.class)); 
     } 
    }); 

    String[] home_items = new String[] {"All", "Categories", "Tags", "Favourites"}; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.single_row_home, R.id.textViewSingleRowHome, home_items); 
    listview_home = (ListView) findViewById(R.id.listViewHome); 
    listview_home.setAdapter(adapter); 
    listview_home.setOnItemClickListener(new OnItemClickListener() 
             { 
              @Override 
              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
              { 
               String selected = ((TextView) arg1.findViewById(R.id.textViewSingleRowHome)).getText().toString(); 

               switch(arg2) 
               { 
                case 0: 

                 Toast.makeText(getBaseContext(), "Yet to be code for "+selected, Toast.LENGTH_SHORT).show(); 
                 break; 

                case 1: 

                 Toast.makeText(getBaseContext(), "Yet to be code for "+selected, Toast.LENGTH_SHORT).show(); 
                 break; 

                case 2: 

                 Toast.makeText(getBaseContext(), "Yet to be code for "+selected, Toast.LENGTH_SHORT).show(); 
                 break; 

                case 3: 

                 Toast.makeText(getBaseContext(), "Yet to be code for "+selected, Toast.LENGTH_SHORT).show(); 
                 break; 
               } 
              } 
             } 
            ); 

    adView = new AdView(this, AdSize.BANNER, "a1512f50d8c3692"); 
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayoutHomeParent); 
    layout.addView(adView); 
    adView.loadAd(new AdRequest()); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
    { 
     showQuitDialog(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

private void showQuitDialog() 
{ 
    AlertDialog.Builder adb = new Builder(this); 
    adb.setTitle("Warning !!!"); 
    adb.setMessage("Are you sure you want to quit ?"); 
    adb.setIcon(R.drawable.ic_launcher); 
    adb.setPositiveButton("Yes",new DialogInterface.OnClickListener() 
            { 
             public void onClick(DialogInterface arg0, int arg1) 
             { 
              if(database.isOpen()) 
               database.close(); 
              finish(); 
             } 
            } 
         ); 

    adb.setNegativeButton("No",new DialogInterface.OnClickListener() 
            { 
             public void onClick(DialogInterface arg0, int arg1) 
             { 
              arg0.dismiss(); 
             } 
            } 
          ); 
    AlertDialog ad = adb.create(); 
    ad.show(); 
} 

@Override 
public void onDestroy() 
{ 
    if (adView != null) 
    { 
     adView.destroy(); 
    } 
    super.onDestroy(); 
} 
} 

我想是吧在頂部(是的那個黑色條),在屏幕的底部。我也爲它寫了,但它顯示輸出爲上面的圖像。

另外我不理解哪一個是AdMob的旗幟,最上面的一個或底部一個??任何人都可以解釋這兩個酒吧的區別。其實我對此很陌生。這是我第一次爲admob編碼。請幫助我學習這一點。

+0

我得到了這個問題。現在告訴您要實現的廣告.. – moDev 2013-03-01 10:11:02

回答

2

只是刪除此:

adView = new AdView(this, AdSize.BANNER, "a1512f50d8c3692"); 
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayoutHomeParent); 
layout.addView(adView); 
adView.loadAd(new AdRequest()); 

你已經在你的XML佈局的AdView的。

相反,這樣做:

adView = (AdView) findViewById(R.id.adView); 
adView.loadAd(new AdRequest()); 

然而,你可能想的是AdRequest中加載廣告之前編輯。這實際上是可選的,取決於你,但如果你這樣做,你的收入將會(據說)增加。

而且,請,之前SO的複製粘貼代碼,首先進行格式化。在Eclipse中,你只需要按下Ctrl + Shift + F

3

兩者都是admob banner。如果您希望在底部展示廣告,請從Activity中刪除以下代碼,因爲您已將代碼添加到底部的xml中。

adView = new AdView(this, AdSize.BANNER, "a1512f50d8c3692"); 
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayoutHomeParent); 
layout.addView(adView); 
adView.loadAd(new AdRequest()); 
+0

但現在它沒有顯示任何旗幟。如果沒有收到來自互聯網的廣告,甚至沒有空白橫幅廣告。找不到橫幅的模擬器屏幕。看看你現在是否可以幫我... – 2013-03-01 13:57:03

+0

如果你不想在空白屏幕頂部,你需要刪除你的相對佈局... – moDev 2013-03-01 17:32:19