带CoordinatorLayout的Snackbar禁用关闭 [英] Snackbar with CoordinatorLayout disable dismiss

查看:88
本文介绍了带CoordinatorLayout的Snackbar禁用关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用支持

  • FloatingActionButton
  • 小吃吧
  • CoordinatorLayout

我需要 CoordinatorLayout ,以便显示 SnackBar 时, FloatingActionButton 会向上移动以为Snackbar腾出空间.为了更好地理解,请查看此视频

我正在使用SnackBar进行双退以退出应用程序,但是可以关闭SnackBar.

有没有办法禁用SnackBar上的关闭功能?

Snackbar snackbar = Snackbar.make(view, R.string.press_back_again_to_exit, Snackbar.LENGTH_SHORT);
snackbar.setAction(R.string.ok, new View.OnClickListener() {
    @Override
    public void onClick(View v)
    {

    }
});
snackbar.setActionTextColor(getResources().getColor(R.color.white));

View view = snackbar.getView();
view.setBackgroundColor(getResources().getColor(R.color.orange_warning));

snackbar.show();

布局

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        xmlns:sothree="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:fitsSystemWindows="true">

        <com.sothree.slidinguppanel.SlidingUpPanelLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            sothree:umanoFadeColor="@android:color/transparent"
            sothree:umanoPanelHeight="100dp"
            sothree:umanoShadowHeight="4dp">

            <!-- Toolbar and main content -->
            <LinearLayout
                android:id="@+id/toolbar_and_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

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

                <!-- Your content layout -->
                <FrameLayout
                    android:id="@+id/content_frame"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"/>

            </LinearLayout>

            <!-- Sliding up panel layout -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/darker_grey"
                android:orientation="vertical">

                ...

            </LinearLayout>

        </com.sothree.slidinguppanel.SlidingUpPanelLayout>


        <!-- Navigation drawer -->
        <ExpandableListView
            android:id="@+id/lv_left_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:childDivider="@android:color/transparent"
            android:clickable="true"
            android:divider="@color/divider_color"
            android:dividerHeight="0.6dp"
            android:fadeScrollbars="true"
            android:groupIndicator="@null"
            android:listSelector="@drawable/button_drawer_child_selector"
            android:scrollbarSize="0dp"/>

    </android.support.v4.widget.DrawerLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginBottom="@dimen/floating_action_button_margin"
        android:layout_marginRight="@dimen/floating_action_button_margin"
        android:src="@drawable/ic_add"
        android:visibility="invisible"
        app:backgroundTint="@color/orange"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal"/>

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

PS

我知道此GitHub库,具有此功能,但是否有本机"怎么做?

解决方案

您可以更改要显示的Snackbar的持续时间.类似于禁用关闭.

int     LENGTH_INDEFINITE   Show the Snackbar indefinitely. 

检查文档.

如果它不起作用 为此,只有一种方法,实现您的自定义Snackbar并覆盖dismiss()方法,而该方法什么也不做. :) dismiss()是公共API.

I am using the support

  • FloatingActionButton
  • Snackbar
  • CoordinatorLayout

I need the CoordinatorLayout so that if SnackBar is shown the FloatingActionButton moves up to make room for the Snackbar. For better understanding check this video.

I am using SnackBar for double-back to exit the application, but the SnackBar can be dismissed.

Is there a way to disable the dismiss on the SnackBar?

Snackbar snackbar = Snackbar.make(view, R.string.press_back_again_to_exit, Snackbar.LENGTH_SHORT);
snackbar.setAction(R.string.ok, new View.OnClickListener() {
    @Override
    public void onClick(View v)
    {

    }
});
snackbar.setActionTextColor(getResources().getColor(R.color.white));

View view = snackbar.getView();
view.setBackgroundColor(getResources().getColor(R.color.orange_warning));

snackbar.show();

Layout

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        xmlns:sothree="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:fitsSystemWindows="true">

        <com.sothree.slidinguppanel.SlidingUpPanelLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            sothree:umanoFadeColor="@android:color/transparent"
            sothree:umanoPanelHeight="100dp"
            sothree:umanoShadowHeight="4dp">

            <!-- Toolbar and main content -->
            <LinearLayout
                android:id="@+id/toolbar_and_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

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

                <!-- Your content layout -->
                <FrameLayout
                    android:id="@+id/content_frame"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"/>

            </LinearLayout>

            <!-- Sliding up panel layout -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/darker_grey"
                android:orientation="vertical">

                ...

            </LinearLayout>

        </com.sothree.slidinguppanel.SlidingUpPanelLayout>


        <!-- Navigation drawer -->
        <ExpandableListView
            android:id="@+id/lv_left_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:childDivider="@android:color/transparent"
            android:clickable="true"
            android:divider="@color/divider_color"
            android:dividerHeight="0.6dp"
            android:fadeScrollbars="true"
            android:groupIndicator="@null"
            android:listSelector="@drawable/button_drawer_child_selector"
            android:scrollbarSize="0dp"/>

    </android.support.v4.widget.DrawerLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginBottom="@dimen/floating_action_button_margin"
        android:layout_marginRight="@dimen/floating_action_button_margin"
        android:src="@drawable/ic_add"
        android:visibility="invisible"
        app:backgroundTint="@color/orange"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal"/>

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

P.S.

I am aware of this GitHub library, that has this functionality, but is there a 'native' way to do it?

解决方案

You can alter the duration of Snackbar to be shown. It will be similar to disable dismiss.

int     LENGTH_INDEFINITE   Show the Snackbar indefinitely. 

Check docs.

if it does not work For this then there is only one way, Implement Your custom Snackbar and override dismiss() method and in that do nothing. :) As dismiss() is a public API.

这篇关于带CoordinatorLayout的Snackbar禁用关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆