【问题标题】:How to make snack bar and fab to move together如何让小吃店和工厂一起移动
【发布时间】:2026-01-17 06:00:01
【问题描述】:

我想像默认行为一样移动小吃店和晶圆厂,但默认情况下,它仅在我们单击晶圆厂时发生,晶圆厂为snackbar 腾出位置,晶圆厂本身向上移动,

但是希望它发生在其他事件发生时,例如布局上喜欢的其他按钮而不是工厂本身,当布局上的其他按钮点击时,我如何触发工厂向上移动并为snackbar 腾出位置

谢谢

点击按钮:

 public void onClick(View view) {
             if(spinner.getText().length() <= 0){
                 showListMenu(spinner);
             }else{
                 showSnack("Added to bag we will hold it for 1 hour!", view.getRootView());
             }
        }

里面的showSnack:

 Snackbar.make(view.getRootView(), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

    final Animation myAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.bounce);
    double animationDuration = 2 * 1000;
    myAnim.setDuration((long)animationDuration);

    MyBounceInterpolator interpolator = new MyBounceInterpolator(0.20, 20.00);

    myAnim.setInterpolator(interpolator);
    fab.startAnimation(myAnim);

【问题讨论】:

  • 请发布您的 Java 代码

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


【解决方案1】:

只需像这样用小吃店锚定工厂:

Snackbar.make(view.findViewById(R.id.fab), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();

【讨论】:

  • blackHawk 的回答有效。只需在 .make(view) 之后删除一个视图
【解决方案2】:

使用CoordinatorLayout 就足够了,无需对Java 代码进行任何更改。在 CoordinatorLayout 中添加您的按钮

    <?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"
    tools:context=".MainActivity">

    <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"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

对于点击事件SnackBarFab 都平滑向上移动

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

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

【讨论】:

    最近更新 更多