在Snackbar上移动cardview - 协调员布局

Roh*_*mar 1 android material-design android-snackbar androiddesignsupport android-coordinatorlayout

我在屏幕底部的cardview中有一个自定义文本视图,并使用快餐栏显示登录时的错误消息.现在,当快餐栏显示时,注册文本视图应该向上移动.我尝试过使用协调器布局,但它不起作用.这是图像在此输入图像描述

des*_*tzq 11

您需要为View实现布局行为,并从布局xml文件中引用它.

实现自己的布局行为很简单.在您的情况下,您只需要在小吃栏出现时设置视图的平移y.

public class YourCustomBehavior extends CoordinatorLayout.Behavior<View> {

    public YourCustomBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        // we only want to trigger the change 
        // only when the changes is from a snackbar
        return dependency instanceof Snackbar.SnackbarLayout;
    }
}
Run Code Online (Sandbox Code Playgroud)

并将其添加到您的布局xml中

<android.support.v7.widget.CardView
        android:id="@+id/your_sign_up_card_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_behavior="com.your.app.YourCustomBehavior"/>
Run Code Online (Sandbox Code Playgroud)

app:layout_behavior属性的值应该是行为类的完全限定名称.

您也可以参考这篇文章,它很好地解释了一切.