2014-04-28 91 views
1

我不知道如何將視圖放到linearLayout的底部。我嘗試使用layout_gravity =「bottom」,但它不起作用。 這是我的代碼:將linear添加到linearLayout的底部

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

回答

0

嗯......你可以通過多種方式解決這個問題,但最簡單的是:

1-更改您的LinearLayout來的FrameLayout和擦除線android:orientation="vertical"

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

3-您可以關注此Tutorial,並忘記將adview包含在xml中,因爲在教程中他們包含以編程方式使用它。

0

我以前總是使用LinearLayout,因爲它非常簡單,但您應該真的考慮使用RealtiveLayout來爲您節省很多麻煩。

RelativeLayout中,您可以設置layout_alignParentBottom=true或只是layout_alignBelo[email protected]+id/thingToBeBelow,廣告將位於正確的位置。

0

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