2012-01-13 40 views
0

我有設計佈局,我必須將admob放在頁面的底部。我無法設置admob底部的頁面。我使用下面的代碼。我如何在Android的頁面上設置admob在底部?

<?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="fill_parent" 
    android:orientation="vertical" > 



    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 

     > 

     <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:paddingTop="30dp" 
     android:orientation="vertical" 
     > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <ImageView 
       android:id="@+id/train" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/atrd" /> 



      <ImageView 
       android:id="@+id/fares" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 

       android:src="@drawable/afrs" /> 

     </TableRow> 
     <TableRow 
       android:id="@+id/tableRow2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

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

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="5dp" 
       android:text="Fares" /> 

      </TableRow> 
      <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_marginTop="5dp" 
      android:layout_height="wrap_content" > 

      <ImageView 
       android:id="@+id/route" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/arts" /> 



      <ImageView 
       android:id="@+id/routemap" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/artms" /> 

     </TableRow> 
     <TableRow 
       android:id="@+id/tableRow2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

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

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="5dp" 
       android:text="Routemap" /> 

      </TableRow> 
      <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_marginTop="5dp" 
      android:layout_height="wrap_content" > 

      <ImageView 
       android:id="@+id/enqury" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/aeqs" /> 



      <ImageView 
       android:id="@+id/information" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ainfs" /> 

     </TableRow> 
     <TableRow 
       android:id="@+id/tableRow2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 

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

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Information" 
       android:paddingLeft="5dp"/> 

      </TableRow> 
    </TableLayout> 
    </LinearLayout> 
    <LinearLayout 
      android:id="@+id/linearLayout2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      > 

      <com.admob.android.ads.AdView 
       android:id="@+id/adview" 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" /> 

     </LinearLayout> 
</LinearLayout> 

誰能告訴我哪裏做錯了。 在此先感謝。

回答

2

採取的RelativeLayout作爲根佈局,然後設置的LinearLayout(包含AdMob的視圖)爲機器人:layout_alignParentBottom = 「真」,例如看到:

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

    <LinearLayout 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:id="@+id/relativeAd" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:gravity="center"> 

<com.google.ads.AdView android:id="@+id/adView" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         ads:adUnitId="@string/admob_id" 
         ads:adSize="BANNER" 
         ads:loadAdOnCreate="true" 
         /> 
     </LinearLayout> 

<!-- other codes here --> 

</RelativeLayout> 

試試這個。

1

您可以在xml的底部聲明一個LinearLayout併爲其指定一個ID。接着寫這個代碼

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // Create the adView 
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID); 

    // Lookup your LinearLayout assuming it’s been given 
    // the attribute android:id="@+id/mainLayout" 
    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); 

    // Add the adView to it 
    layout.addView(adView); 

    // Initiate a generic request to load it with an ad 
    adView.loadAd(new AdRequest()); 
} 

@Override 
public void onDestroy() { 
    adView.destroy(); 
    super.onDestroy(); 
} 
0

你也可以在你的代碼中使用android:layout_weight="1"屬性linearLayout1

像這樣:

<LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <TableLayout 
      ........ 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <com.admob.android.ads.AdView 
      android:id="@+id/adview" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

這可以解決你的問題,而不會干擾你的代碼。