使Snackbar将视图向上推

17

是否有一种方法使 Snackbar 在显示时将视图向上推送?

snackbar

目前,当 Snackbar 显示时,它会覆盖下面的视图,因此看起来像按钮的一半被切断了,但我希望它几乎可以像软键盘在屏幕上显示时那样进行 "adjustPan"。

有人知道任何技巧可以实现这一点吗?

4个回答

24
将您的视图放在一个android.support.design.CoordinatorLayout内。
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true">

    <View
        android:id="@+id/my_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="com.example.FloatingActionButtonBehavior"/>

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

这里重要的部分是 app:layout_behavior 属性。这是实现类:

public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {

    public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {

        return dependency instanceof SnackbarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {

        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }
}

然后当你展示 Snackbar 时,将引用传递给 CoordinatorLayout:

CoordinatorLayout coordinator = findViewById(R.id.coordinator);
Snackbar.make(coordinator, textResId, Snackbar.LENGTH_LONG)
        .setAction(R.string.accept_ok, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        })
        .setDuration(Snackbar.LENGTH_INDEFINITE)
        .show();

希望这能对您有所帮助。


1
谢谢 - 但是我在我的CoordinatorLayout中使用的是viewGroup(LinearLayout),而不是view。有没有办法将您的代码与viewgroup一起使用?我尝试了linearLayout,但它给出了一个错误,说android.widget.LinearLayout无法强制转换为android.support.design.widget.FloatingActionButton。 - Simon
1
我使用了这个解决方案来推动我的RelativeLayout,效果非常好 :) @Simon 你是否忘记在两个重写的方法中将FloatingActionButton更改为LinearLayout? - Bruce
2
我只想指出,即使空构造函数看起来毫无用处,如果您不声明它,在设计视图中进行布局预览时会出现异常。 - Joao Sousa
2
使用此解决方案,如果您正在使用滑动以关闭 snackbar 功能,则您的布局将保持在新位置。要返回到旧位置,您可以使用以下代码:snackbar.setCallback(new Snackbar.Callback() { @Override public void onDismissed(Snackbar snackbar, int event) { super.onDismissed(snackbar, event); yourLayout.setTranslationY(0.0f); } }); - uuzelac
1
我想补充一点,你需要创建自定义行为。因此,通常情况下,它看起来像CoordinatorLayout.Behavior<T>,其中T是子视图(在Snackbar或其他依赖项的情况下需要移动的视图)。 - Mikhail Valuyskiy
显示剩余6条评论

3

简短回答:如果你只想在 Snackbar 出现时将 View 向上推送,只需将以下行添加到你的 View 中即可。不需要再定义 FloatingActionButtonBehavior 了。

app:layout_dodgeInsetEdges="bottom"

长答案:已接受的答案中描述的FloatingActionButtonBehavior确实可以完成工作。
我注意到的一件事是,如果我有一个没有FloatingActionButtonBehaviorCoordinatorLayout和一个真正的FloatingActionButton,那么当Snackbar出现时,FAB会感觉稍微“弹跳”。但是,如果我使用其他东西,比如ConstraintLayout以及FloatingActionButtonBehavior,那么“弹跳”感就不存在了;Snackbar上方的视图只是在底部留出足够的空间供Snackbar使用,而Snackbar逐渐填充该空间。这是相当微妙的,我相信大多数人甚至不会注意到它或者不会关心它。
这是Snackbar开始出现的时候。请注意,它的大小略小于其完整大小。 enter image description here 然后,在几分之一秒后,Snackbar“扩展”到其完整大小。 enter image description here 这就是导致“弹跳”感的原因;FAB的位置基于Snackbar的当前大小,而不是Snackbar的完全显示大小。
要复制此行为,只需让FloatingActionButtonBehavior知道在哪里锚定FAB,而不是自己计算Y坐标。更好的方法是参考“简短答案”。
/*
Replicate real FAB's behavior. Code shamelessly stolen from
com/google/android/material/floatingactionbutton/FloatingActionButton.java
*/
class FloatingActionButtonBehavior(
    context: Context,
    attrs: AttributeSet
) : CoordinatorLayout.Behavior<View>() {
    override fun onAttachedToLayoutParams(params: CoordinatorLayout.LayoutParams) {
        super.onAttachedToLayoutParams(params)

        if (params.dodgeInsetEdges == Gravity.NO_GRAVITY) {
            params.dodgeInsetEdges = Gravity.BOTTOM
        }
    }
}

app:layout_dodgeInsetEdges="bottom" 解决方案实际有效,不像是一种 hack。 只需要将其放置在最上层的布局视图中,例如对于一个活动多个片段应用程序,在 activity_main.xml 协调布局中放置即可。 - Petr Krýže

2
他是 SourceRebeles 在 Kotlin 中使用泛型的解决方案。
typealias T = View

class FloatingActionButtonBehavior(context: Context, attrs: AttributeSet) : CoordinatorLayout.Behavior<T>() {

    override fun layoutDependsOn(parent: CoordinatorLayout, child: T, dependency: View): Boolean {

        return dependency is SnackbarLayout
    }

    override fun onDependentViewChanged(parent: CoordinatorLayout, child: T, dependency: View): Boolean {

        val translationY = Math.min(0.0f, (dependency.translationY - dependency.height).toFloat())
        child.translationY = translationY
        return true
    }
}

0

enter image description here

Snackbar 会将最近的 CoordinatorLayout 作为 parentView

 findViewById(R.id.tv_main).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(findViewById(R.id.tv_main), "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            toolbar.getChildAt(1).setVisibility(View.GONE);
        }
    });

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
    android:text="s"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_marginBottom="200dp"
        android:layout_height="100dp">
        <TextView
            android:id="@+id/tv_main"
            android:background="#4b14b1"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接