[英]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.