【问题标题】:How to get Adview below Viewpager in CoordinatorLayout如何在 CoordinatorLayout 中的 Viewpager 下方获取 Adview
【发布时间】:2015-09-09 04:21:21
【问题描述】:

目前AdView 出现在ViewPager 中,因此它阻止了应用程序中的内容。我怎样才能让AdView 出现在ViewPager 下面而不是里面。

我试图将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>

【问题讨论】:

    标签: android android-recyclerview android-coordinatorlayout


    【解决方案1】:

    将 ViewPager 和 Adview 放在另一个 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"/>
    
            <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>
    

    【讨论】:

    • 我编辑了我的答案。之前忘了提到LinearLayout。
    • 请为查看寻呼机圆形页面指示器和下方带有浮动操作按钮示例的回收站视图提供一些 xml 布局
    • 这是最好的答案。为我工作!
    • 很好的答案!
    【解决方案2】:

    我尝试了@AvishekV 的答案,但这并没有解决我的问题。但是我采用了将 CoordinatorLayout 和 Adview 放在父布局中的想法,在我的例子中是 ConstraintLayout。下面是结构

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
       <android.support.design.widget.CoordinatorLayout
           android:id="@+id/content"
           android:layout_width="0dp"
           android:layout_height="0dp"
           android:fitsSystemWindows="true"
           app:layout_constraintBottom_toTopOf="@+id/adView"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintTop_toTopOf="parent">
    
            <android.support.design.widget.AppBarLayout>
               //CollapsingToolbarLayout
               //ConstraintLayout, if you need to put anything inside collpasingtoolbar
               //Toolbar
               //TabLayout
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.view.ViewPager
            android:id="@+id/container"
            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="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="your adunit id"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      您可以在RelativeLayout 中添加ViewPagerAdView,并将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>
      

      【讨论】:

      • 这样做的问题是,现在它会将内容向上和向下推送到工具栏的大小,所以我认为它是不想一起玩的新协调器布局。我真的不希望它嵌套在 viewpager 中,但我想到目前为止唯一的解决方案是将横幅大小底部填充放在我的 recyclerview 上
      • 不,它仍然在底部的 viewpager 内,但没关系,只需使用 recyclerview 上的底部填充,因此当您滚动到底部时,广告会有一个空白区域
      • 你把这行 android:layout_above="@+id/adView" 添加到 CoordinatorLayout 了吗?它不可能在 ViewPager 里面。我已经在一个示例应用程序中进行了测试:)
      • 糟糕,我没看到。是的,现在它完美无缺,非常感谢
      • 它可以工作,但我在状态栏中得到一个白色覆盖,如这个问题所示:stackoverflow.com/q/32428700/1820644
      【解决方案4】:

      当我在对我有用的真实设备上进行测试时:Fix Height Declear On Bottom Child `

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

      希望能帮到你

      【讨论】:

        【解决方案5】:

        这对我有用: 只需将 app:layout_behavior 放在 ViewPager 中:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-09
          • 1970-01-01
          • 1970-01-01
          • 2015-08-21
          • 1970-01-01
          • 2015-08-28
          相关资源
          最近更新 更多