您需要为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文章,它很好地解释了所有内容。
这是我在有人监视的情况下所做的。
一种更简单的方法