2015-06-23 26 views
30

目前AdView出現在ViewPager的內部,因此它阻止了應用中的內容。我怎樣才能讓AdView出現在ViewPager的下面,而不是裏面。如何在CoordinatorLayout的Viewpager下面獲取Adview

我試圖將AdView放在ViewPager下面的RelativeLayout中,但之後AdView完全不顯示。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.candyx.testapp.Activity"> 

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/main_button_background"> 

     <android.support.v7.widget.Toolbar 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways"/> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabMode="scrollable" 
      app:tabGravity="fill" 
      app:tabIndicatorColor="@color/colorTabIndicator" 
      app:tabIndicatorHeight="3dp"/> 

    </android.support.design.widget.AppBarLayout> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

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

</android.support.design.widget.CoordinatorLayout> 

</LinearLayout> 

回答

77

您可以添加ViewPagerAdViewRelativeLayout並指定android:layout_above="@+id/adView"屬性ViewPager使adView不會阻止ViewPager內容。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.candyx.testapp.Activity"> 

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/adView"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/main_button_background"> 

      <android.support.v7.widget.Toolbar 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_scrollFlags="scroll|enterAlways"/> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:tabMode="scrollable" 
       app:tabGravity="fill" 
       app:tabIndicatorColor="@color/colorTabIndicator" 
       app:tabIndicatorHeight="3dp"/> 

     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    </android.support.design.widget.CoordinatorLayout> 

    <com.google.android.gms.ads.AdView 
     android:id="@id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" 
     ads:adUnitId="@string/banner_ad_unit_id" 
     android:layout_gravity="center|bottom" 
     android:layout_alignParentBottom="true"/> 

</RelativeLayout> 
+1

這個問題是,現在它推動了內容和工具欄下的橫幅大小,所以我認爲它的新協調員佈局,不想一起玩。我真的不希望它嵌套在viewpager裏面,但我想現在唯一的解決方案是在我的recyclerview上放置橫幅大小的底部填充 –

+0

檢查編輯..... –

+0

沒有它仍然在viewpager的底部,但它沒關係只是使用recyclerview的底部填充,所以當你滾動到底部有一個空白的廣告 –

3

把ViewPager和AD瀏覽一個又一個的LinearLayout如下內:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
    <android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      android:layout_weight="1"/> 

     <com.google.android.gms.ads.AdView 
      xmlns:ads="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/adView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      ads:adSize="BANNER" 
      ads:adUnitId="@string/banner_ad_unit_id" 
      android:layout_gravity="center|bottom" 
      /> 
    </LinearLayout> 
+0

還沒有在viewpager裏面顯示 –

+0

我編輯了我的答案。之前忘記提及LinearLayout。 – Audumbar

+0

就像什麼都沒有顯示一切只是空白 –

1

這爲我工作: 只是把ViewPager裏面的應用程序:layout_behavior:

<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
0

我測試的多數民衆贊成在工作的實際設備對我來說:修復身高Declear在底部兒童 `

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/htab_maincontent" 
    android:layout_width="match_parent" 
    android:layout_above="@+id/PanelView" 
    android:layout_height="match_parent"> 
    <!--android:fitsSystemWindows="false">--> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/htab_appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/htab_collapse_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="256dp" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:id="@+id/htab_header" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@drawable/title_panel_main" 
       android:fitsSystemWindows="true" 
       android:scaleType="centerCrop" 
       app:layout_collapseMode="parallax" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/htab_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="104dp" 
       android:gravity="top" 
       android:minHeight="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:titleMarginTop="13dp" /> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/htab_tabs" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:layout_gravity="bottom" 
       app:tabIndicatorColor="@android:color/white" /> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/tab_viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 


<LinearLayout 
    android:id="@+id/PanelView" 
    android:layout_width="match_parent" 
    android:layout_height="70dp" 
    android:orientation="horizontal" 
    android:padding="7dp" 
    android:layout_alignParentBottom="true" 
    android:visibility="visible"> 



    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     /> 
</LinearLayout> 
</RelativeLayout>` 

希望幫助您

相關問題