繁体   English   中英

将广告添加到linearLayout的底部

[英]Adding ad to the bottom of linearLayout

我不知道如何将视图置于linearLayout的底部。 我尝试使用layout_gravity =“ bottom”,但是它不起作用。 这是我的代码:

<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>

嗯...您可以通过多种方式解决此问题,但最简单的方法是:

1-将您的LinearLayout更改为FrameLayout并删除行android:orientation="vertical"

2-将其添加到您的LinearLayout android:gravity="bottom"

3-您可以按照本教程进行操作,而忘记在XML中包含adview,因为在本教程中他们以编程方式将其包括在内。

我以前总是使用LinearLayout因为它是如此简单,但是您应该真正考虑使用RealtiveLayout来省去很多麻烦。

RelativeLayout您可以设置layout_alignParentBottom=true或仅将layout_alignBelow=@+id/thingToBeBelow设置为正确位置。

使用LinearLayout作为父对象,您需要创建一个容器布局来填充AdView上方的其他区域,如下所示。 您可以在容器布局中添加所有其他子视图或将其保留为空。

 <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>

另一种方法是将RelativeLayout用作父对象,并将AdView置于父对象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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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