使用TranslucentNavigation的Snackbar和CollapsingToolbarLayout

时间:2016-12-11 05:39:49

标签: android floating-action-button android-collapsingtoolbarlayout android-snackbar

我已经使用Android Studio Templets中的滚动活动创建了一个应用来测试我的主应用程序的编码是否会影响该行为,所以我只是添加到代码中:

<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>

显示导航栏后面的Snackbar作为此屏幕截图(PS:我正在使用Xstane在我的手机上重新设计我的导航栏,但我认为这不会影响代码因为我尝试了TranslucentNavigation与Snackbar没有CollapsingToolbarLayout并且有效孔)

enter image description here

该应用正在支持

  • windowTranslucentNavigation
  • 小吃吧
  • CollapsingToolbarLayout
  • FloatingActionButton

这是主xml的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".testscanbarwithcollapsing.ScrollingActivity"
>

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay"
    >

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            />

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

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

<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"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@android:drawable/ic_dialog_email"
    />

更新:这是显示Snackbar的FloatingButton的onClick代码(此代码位于主要活动的onCreate中)

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fabProgressCircle.show();
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

1 个答案:

答案 0 :(得分:8)

    private static void show(final Activity activity, Snackbar snackbar) {
    if (ScreenUtils.isTranslucentNavigationBar(activity)){
        final FrameLayout snackBarView = (FrameLayout) snackbar.getView();
        final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) snackBarView.getChildAt(0).getLayoutParams();
        params.setMargins(params.leftMargin,
                params.topMargin,
                params.rightMargin,
                params.bottomMargin + ScreenUtils.getNavigationBarHeight(activity));

        snackBarView.getChildAt(0).setLayoutParams(params);
    }
    snackbar.show();
}

public static boolean isTranslucentNavigationBar(Activity activity) {
    Window w = activity.getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    int flags = lp.flags;
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
            && (flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            == WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

}

这个理想对我有用!