在Snackbar上移动名片视图-协调器布局

问题描述 投票:1回答:3

我在屏幕底部的卡片视图中有一个自定义的文本视图,并使用点心栏在登录时显示错误消息。现在,当点心栏显示时,注册的文本视图应上移。我尝试使用协调器布局,但是它不起作用。这是图像enter image description here

android material-design androiddesignsupport android-coordinatorlayout android-snackbar
3个回答
11
投票

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

实现您自己的布局行为很简单。就您而言,只需要在小吃栏出现时设置视图的翻译即可。

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

并像这样将其添加到您的布局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"/>

app:layout_behavior属性的值应为行为类的全限定名。

您也可以参考this文章,它很好地解释了所有内容。


2
投票

这是我在有人监视的情况下所做的。


0
投票

一种更简单的方法

© www.soinside.com 2019 - 2024. All rights reserved.