简体   繁体   中英

Adding ad to the bottom of linearLayout

I don't know how to put a view to the bottom of linearLayout. I tryed using layout_gravity="bottom", but it doesn't work. This is my code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background_register_login"
    tools:context="com.mcd.fantaleghe.MainActivity$PlaceholderFragment" >


     <com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_gravity="bottom"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"/>

</LinearLayout>

Well... you could solve this problem in several ways, but the easiest are:

1- Change your LinearLayout to FrameLayout and erase the line android:orientation="vertical"

2- Add this to your LinearLayout android:gravity="bottom"

3- You could follow this Tutorial and forget about including the adview in the xml because in the tutorial they include it programmatically.

I used to always use LinearLayout because it is so simple, but you should really consider using a RealtiveLayout to save yourself a lot of headaches.

In a RelativeLayout you could set layout_alignParentBottom=true or just layout_alignBelow=@+id/thingToBeBelow and the ad will be in the right place.

Using LinearLayout as parent , You need to create a container layout to fill the other area above the AdView like this. You can add all your other child views inside the container layout or leave it empty.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
<LinearLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="2"/>

<com.google.android.gms.ads.AdView
    android:id="@+id/welcomeAdView"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_weight="1"
    ads:adSize="SMART_BANNER"
    ads:adUnitId=""/>
</LinearLayout>

Another , way is to use RelativeLayout as parent and place the AdView to parent bottom

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:ads="http://schemas.android.com/apk/res-auto">

<com.google.android.gms.ads.AdView
    android:layout_alignParentBottom="true"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId=""/>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM