使用 CoordinatorLayout 时 AdView 和 FloatingActionButton 重叠

AdView and FloatingActionButton overlap while using CoordinatorLayout(使用 CoordinatorLayout 时 AdView 和 FloatingActionButton 重叠)

本文介绍了使用 CoordinatorLayout 时 AdView 和 FloatingActionButton 重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个布局中加入新的 CoordinatorLayout 后,我遇到了 AdviewFloatingActionButton 重叠的问题.

After incorporating the new CoordinatorLayout in one of my layouts, I have an issue with Adviews overlapping with FloatingActionButton.

<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:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </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.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_done" />

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

来源: https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml

如果我采用相同的布局并在 FloatingActionButton 下方合并一个 AdView 它会导致 FloatingActionButton 不再与 一起使用小吃店:

If I take the same layout and incorporate an AdView below the FloatingActionButton it causes the FloatingActionButton to no longer work with the SnackBars:

<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:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </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.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_done" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/ad_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom"
        app:adSize="SMART_BANNER"
        app:adUnitId="@string/app_ad" />

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

可重现的简单 Admob 示例:

    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    ((AdView)findViewById(R.id.ad_view)).loadAd(adRequest);

我有两个问题:

  • 重叠视图(很可能是因为 CoordinatorLayout 扩展了 FrameLayout)
  • 在与 FloatingActionButton 相同的布局中添加其他视图会导致它不再与 SnackBar
  • 移动或协调"

    I have two issues:

    • Overlapping Views (most likely because CoordinatorLayout extends FrameLayout)
    • Adding other views in the same layout as the FloatingActionButton cause it to no longer to move or "coordinate" with the SnackBar
    • 推荐答案

      发布后,我决定尝试将整个 CoordinatorLayout 布局包装在一个 LinearLayout 中,看起来好好工作.也许有人知道更好的方法?

      After posting, I decided to try and wrap the entire CoordinatorLayout layout in a LinearLayout and it seems to work well. Maybe someone knows of a better way?

      我不确定这是不是最好的方法,因为有很多嵌套视图:

      I am not sure this is the best way as there are many nested views:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="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"
                    tools:context=".activities.MainActivity">
      <android.support.design.widget.CoordinatorLayout
          android:id="@+id/main_content"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:background="?attr/colorPrimary"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                  app:layout_scrollFlags="scroll|enterAlways" />
      
              <android.support.design.widget.TabLayout
                  android:id="@+id/tabs"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
      
          </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.FloatingActionButton
              android:id="@+id/fab"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="end|bottom"
              android:layout_margin="@dimen/fab_margin"
              android:src="@drawable/ic_done" />
      
          <com.google.android.gms.ads.AdView
              android:id="@+id/ad_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center|bottom"
              app:adSize="SMART_BANNER"
              app:adUnitId="@string/app_ad" />
      
      </android.support.design.widget.CoordinatorLayout>
      </LinearLayout>
      

      这篇关于使用 CoordinatorLayout 时 AdView 和 FloatingActionButton 重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 CoordinatorLayout 时 AdView 和 FloatingActionButton 重叠

基础教程推荐