使Snackbar向上推视图 [英] Make Snackbar push view upwards

查看:102
本文介绍了使Snackbar向上推视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使小吃栏在显示时将视图向上推?

Is there a way to make the snackbar push the view upwards when it is shown?

当前,显示快餐栏时,它覆盖下面的视图,因此看起来好像按钮的一半被切断了,但是我希望它几乎"adjustPan",类似于软键盘在屏幕上显示时的工作方式

Currently, when the snackbar is shown, it overlays the view below so it looks like half the button is cut off but I want it to almost "adjustPan" similar to the way that the softkeyboard works when it is shown on the screen.

有人知道实现这一目标的任何技巧吗?

Does anyone know of any tricks to achieve this?

推荐答案

将视图放入android.support.design.CoordinatorLayout

Put your view inside a 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属性.这是实现类:

The important part here is app:layout_behavior attribute. Here is implementing class:

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;
    }
}

然后,当显示小吃栏时,将对CoordinatorLayout的引用传递给

Then when you show the snackbar pass the reference to the 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();

希望这会对您有所帮助.

Hope this will help you.

这篇关于使Snackbar向上推视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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