簡體   English   中英

Snackbar在按下動作按鈕時隱藏了浮動動作

[英]Snackbar hides the Floating Action on pressing the action button on it

我正在研究Snack bar和Floating Action按鈕。 我使用協調器布局使浮動操作按鈕在顯示快餐欄時顯示/移動。 問題是我為小吃店保留了一個動作。 點擊浮動按鈕時,Snackbar會彈出,並且浮動操作按鈕會向上移動。 當我按下快餐欄動作項時,浮動動作按鈕隱藏在兒童零食欄下面。

而且如果我連續按下浮動動作按鈕,那么浮動動作按鈕也會被隱藏。

以下是我的代碼。

activity_main.xml中

<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"
tools:context="com.example.dev.firsttest.Screen2"
>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary_color"></android.support.v7.widget.Toolbar>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/coordinatorlayout">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/searchfab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_add_black_24dp"
        app:fabSize="normal">

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

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

主要活動

Toolbar toolbar;
FloatingActionButton searchfab;
CoordinatorLayout coordinatorLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen2);

    toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinatorlayout);

    searchfab = (FloatingActionButton)findViewById(R.id.searchfab);
    searchfab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(coordinatorLayout, "This is Snackbar Demo", Snackbar.LENGTH_LONG).setAction("Click", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Snackbar.make(coordinatorLayout, "This is Child Snackbar", Snackbar.LENGTH_LONG).show();
                }
            }).show();
        }
    });


}

在Snackbar中按子操作並在浮動操作按鈕上連續點擊會使浮動操作按鈕隱藏回Snackbar

感謝您的幫助

謝謝

答案在這里: https//github.com/ggajews/coordinatorlayoutwithfabdemo

當快餐欄顯示時,它將移動FAB。

我無法重現您的問題。 這是我的代碼

View.OnClickListener test1 = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(getActivity().findViewById(R.id.snackbarPosition), "test 1", Snackbar.LENGTH_LONG)
                        .setAction(R.string.snackbar_action_undo, new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                 Snackbar.make(getActivity().findViewById(R.id.snackbarPosition), "test 2", Snackbar.LENGTH_LONG)
                                        .setActionTextColor(getResources().getColor(R.color.myBlueGreen))
                                        .show();
                            }
                        })
                        .show();
            }
        };

FloatingActionButton button = (FloatingActionButton)streamView.findViewById(R.id.buttonFloat);
button.setOnClickListener(test1); 

XML

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@android:color/white"
    android:id="@+id/snackbarPosition">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/buttonFloat"
        android:src="@drawable/ic_content_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        app:backgroundTint="@color/myBlueGreen"

        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        />

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

添加第二個快餐欄“test2”在小吃欄“test1”上顯示為OnClickListener,對我來說,用“test2”替換小吃欄“test1”(並且不隱藏“浮動”按鈕)

此外,在“浮動操作按鈕”上單擊兩次會使快餐欄“test1”閃爍,即兩次出現兩次,而不是兩個小吃欄。 浮動操作按鈕不會消失。

換句話說,我從來沒有在“父母”小吃店頂部看到一個“兒童”小吃店。

我看不出你和我的代碼之間的區別。 也許嘗試復制我的代碼,看看它是否解決了你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM