【问题标题】:Snackbar and CollapsingToolbarLayout with TranslucentNavigation带有半透明导航的 Snackbar 和 CollapsingToolbarLayout
【发布时间】:2017-04-26 06:49:46
【问题描述】:

我已经从 Android Studio 模板中创建了一个带有 Scrolling Activity 的应用程序来测试我的主应用程序的编码是否会影响该行为,所以我只是添加到代码中:

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

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

应用支持

  • windowTranslucentNavigation
  • 小吃店
  • CollapsingToolbarLayout
  • 浮动操作按钮

这是主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();
        }
    });

【问题讨论】:

  • 请显示代码你是如何显示小吃店的。
  • @VipulAsri 更新

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


【解决方案1】:
    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;

}

这个理想适合我!

【讨论】:

  • 非常感谢您发布您对一个未回答的旧问题的答案:D
  • 我试过了,现在无法解析符号'ScreenUtils',你能参考这个库的资源吗,因为我找不到它
  • 它是我的 util 类。查看使用类 ScreenUtils 的方法名称并使用 Google :)
  • 这肯定会降低我的小吃店的高度,但小吃店的消息也会消失 :(
  • @AlexandrLarin 如果无法访问活动怎么办? (View 上的扩展功能,用于显示小吃吧)
猜你喜欢
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多