具有Adview的Android浮动操作按钮行为

时间:2016-06-08 16:39:20

标签: android-layout

我很抱歉,如果这已经得到了解答,但已经有一段时间了,我还在搜索。由于FAB有一个行为类,您可以分配它,它将在协调器布局内滚动,我想知道是否可以包含行为,使FAB自动放在adview上方,当它可见时类似于它对小吃店做出反应。提前谢谢。

1 个答案:

答案 0 :(得分:0)

解决方法!

我想出了一个解决方法,因为我刚刚回来玩,甚至忘记了这个问题。我没有将我的广告视图放在包含其余视图的容器中,而只需将协调器视图包装在相对布局中,并将协调器布局设置在广告ID上方。我确信这可能不是最好的方法。

所以我最终得到的是这样的:NB我的广告视图设置为默认情况下可见性,我只在广告请求完成时设置了可见性

<RelativeLayout
    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.support.design.widget.CoordinatorLayout
    android:layout_above="@+id/ad_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:visibility="gone"
        app:layout_behavior="utils.FabBehavior_Main"
        app:rippleColor="@color/fab_ripple"
        app:srcCompat="@drawable/ic_fab" />

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

    <com.google.android.gms.ads.AdView
    android:id="@+id/ad_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:visibility="gone"
    app:adSize="SMART_BANNER"
    app:adUnitId="@string/banner_ad_unit_id"
    />

</RelativeLayout>

utils.FabBehavior_Main是相对包类,它包含视图的行为属性,utils是包。所以在你自己的应用程序中,你可以使用名称空间com.myapp.utils.ScrollAwareFab,它可能是这样的:

public class ScrollAwareFab extends FloatingActionButton.Behavior {

    public ScrollAwareFab(Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
                                       FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
                super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
                                          nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
                               View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
                             dyUnconsumed);

        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
                /**
                 * Called when a FloatingActionButton has been hidden
                 *
                 * @param fab the FloatingActionButton that was hidden.
                 */
                @Override
                public void onHidden(FloatingActionButton fab) {
                    super.onShown(fab);
                    fab.setVisibility(View.INVISIBLE);
                }
            });
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            child.show();
        }
    }
}
相关问题