2014-05-08 2 views
0

Я добавляю свое объявление программно вверху каждого макета. В LinearLayout все работает нормально, но в RelativeLayouts объявление перекрывает контент.AdMob banner overlaps layout content

// activity 
AdHandler.setAd(this, (RelativeLayout) findViewById(R.id.activity_bet), null); 

// handler 
public class AdHandler { 
    private static AdView adView; 

    public static void setAd(Context context, RelativeLayout relLayout, LinearLayout linLayout) { 
     // Create the adView. 
     adView = new AdView(context); 
     adView.setAdUnitId(context.getString(R.string.ad_unit_id)); 
     adView.setAdSize(AdSize.BANNER); 
     // Add the adView to it. 
     if (relLayout != null) { 
      relLayout.addView(adView, 0); 
     } 
     else 
      linLayout.addView(adView, 0); 

     // Initiate a generic request. 
     AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("034CB02678DF7E469581XXXXX").build(); 

     // Load the adView with the ad request. 
     adView.loadAd(adRequest); 
    } 
} 

EDIT:

Моя раскладка:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.example.myfirstapp.LoginActivity" > 
    <!-- Loading progress --> 

    <LinearLayout 
     android:id="@+id/loading_status" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" /> 

     <TextView 
      android:id="@+id/loading_status_message" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:fontFamily="sans-serif-light" 
      android:text="@string/initial_loading_status_message" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 
    </LinearLayout> 

    <RelativeLayout 
      android:id="@+id/activity_bet" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_below="@+id/adPlaceholder"> 

      <RelativeLayout 
      android:id="@+id/header" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/quoteLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Quote:" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
      <TextView 
       android:id="@+id/quote" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@+id/quoteLabel" 
       android:layout_marginLeft="3dp" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <TextView 
       android:id="@+id/dateLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Date:" 
       android:layout_below="@+id/quote" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
      <TextView 
       android:id="@+id/date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/quote" 
       android:layout_toRightOf="@+id/dateLabel" 
       android:layout_marginLeft="3dp" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <TextView 
       android:id="@+id/comment" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/dateLabel" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <TextView 
       android:id="@+id/username" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 

      <ImageView android:id="@+id/verified" 
       android:contentDescription="@string/verified" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toLeftOf="@+id/username" 
       android:src="@drawable/ic_accept" 
       android:layout_marginTop="4dp" 
       android:layout_marginLeft="2dp" 
       android:visibility="gone" />  

      <ImageView 
       android:id="@+id/avatar" 
       android:layout_width="64dp" 
       android:layout_height="64dp" 
       android:layout_below="@+id/username" 
       android:contentDescription="Avatar" 
       android:layout_alignParentRight="true" /> 

      <Button 
       android:id="@+id/profile" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Go to Profile" 
       android:layout_below="@+id/avatar" 
       android:layout_alignParentRight="true" /> 

      <ProgressBar 
       android:id="@+id/subscribeLoading" 
       style="?android:attr/progressBarStyle" 
       android:layout_width="wrap_content" 
       android:layout_below="@+id/avatar" 
       android:layout_alignParentRight="true" 
       android:layout_height="wrap_content" 
       android:visibility="gone" /> 
      </RelativeLayout>   

      <TextView android:id="@+id/bet_listview_header" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="#EE2C2C" 
        android:textSize="16sp" 
        android:padding="2dp" 
        android:textStyle="bold" 
        android:textColor="#FFFFFF" 
        android:layout_below="@+id/header" 
        android:text="@string/bet_listview_header" /> 

      <ListView 
       android:id="@+id/bet_listview" 
       android:layout_below="@+id/bet_listview_header" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
      </ListView> 
    </RelativeLayout> 
</merge> 
+0

Вы только что обнаружили существенное различие между линейной и относительной компоновкой. Отправьте свой .xml, и я исправлю его для вас. –

+0

где вы хотите, чтобы макет объявления был? –

+0

использовать admob outsite макета, вы добавляете текстовое представление и все такое. и просто добавьте android: alignparenttop в admob и android: layout_below = "admob" n ваш относительный макет. – Zohaib

ответ

1

Я предлагаю макет так:

<?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 
     android:id="@+id/adPlaceholder" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/rlOtherContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/adPlaceholder" > 
    </RelativeLayout> 

</RelativeLayout> 

LinearLayout "adPlaceholder" останется пустым ООН пока вы не добавите к нему Баннер. Если добавлен баннер, обратите внимание на «layout_below» свойство RelativeLayout, представляющее основное содержание. Он автоматически выровняется ниже знака , предотвращая его перекрытие.

Пример кода:

LinearLayout placeholder = (LinearLayout) findViewById(R.id.adPlaceholder); 
placeholder.addView(AdView); 

Если вы хотите разместить баннер ниже RelativeLayout основного содержания, просто изменить порядок в .xml файле и использовать «layout_above» свойство в RelativeLayout , а не «layout_below».

+0

Я попытался встать над макетом 'activity_bet', но все тот же результат. Я думаю, это из-за ''. – Chris

+0

В принципе, вы можете скопировать файл макета, который я разместил, и поместить ваш макет внутри моего «rlOtherContent». Затем используйте код, который я написал выше, чтобы раздуть объявление. –

+0

Кажется, отлично работает, спасибо. – Chris

Смежные вопросы