AdView и FloatingActionButton перекрываются при использовании CoordinatorLayout

После включения нового CoordinatorLayout в один из моих макетов у меня возникла проблема с перекрытием Adview с 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

Если я возьму тот же макет и включу AdView под FloatingActionButton, это приведет к тому, что FloatingActionButton больше не будет работать с 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

Картинки:

Перекрытие

Без анимации

Как я могу реализовать это правильно?


person Jared Burrows    schedule 06.06.2015    source источник
comment
Почему за это проголосовали?   -  person Jared Burrows    schedule 27.06.2015
comment
Похоже, правильный вопрос, и ваш запрос решил мою другую проблему. Спасибо. +1 за ваш вопрос   -  person Naruto    schedule 27.06.2015
comment
@Наруто Спасибо. Это затронуло некоторые мои приложения, и я решил попытаться спросить сообщество. Мне не нравятся все вложенные макеты.   -  person Jared Burrows    schedule 27.06.2015


Ответы (2)


После публикации я решил попытаться обернуть весь макет CoordinatorLayout в макет LinearLayout, и, похоже, он работает хорошо. Может кто знает способ лучше?

Я не уверен, что это лучший способ, так как существует много вложенных представлений:

<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>
person Jared Burrows    schedule 06.06.2015
comment
Возможно, вы можете использовать FrameLayout вместо LinearLayout, он должен быть более легким. - person Sergii; 11.12.2015
comment
Это решение хорошее, но у меня есть совпадения с ним :( - person Avi Parshan; 25.03.2017

Вероятно, это не то, что вы искали, но вы можете, по крайней мере, выровнять FloatingActionButton, вот так

<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.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_done"
    app:layout_anchor="@id/adView"
    app:layout_anchorGravity="top|end|right"
/>

Так как CoordinatorLayout это FrameLayout, вы хотите, чтобы fab был после AdView, т.к.

Дочерние представления рисуются в стеке, причем самый последний добавленный дочерний элемент находится сверху.

потрясающе сосредоточено на рекламе

person serv-inc    schedule 18.07.2018