CoordinatorLayout|CoordinatorLayout --自定义Behavior 1

1.加入com.android.support:design:24.1.1包引用

CoordinatorLayout|CoordinatorLayout --自定义Behavior 1
文章图片
image.png 2.自定义 CoordinatorLayout.Behavior
public class EasyBehavior extends CoordinatorLayout.Behavior {//这里的泛型是child的类型,也就是观察者View
public EasyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, TextView child, View dependency) {
//告知监听的父控件是Button
return dependency instanceof Button;
}
@Override
//当 dependency(Button)变化的时候,可以对child(TextView)进行操作
public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {
child.setX(dependency.getX()+200);
child.setY(dependency.getY()+200);
child.setText(dependency.getX()+","+dependency.getY());
return true;
}
}
3.加载这个自定义的Behavior(注意包名要写对)

CoordinatorLayout|CoordinatorLayout --自定义Behavior 1
文章图片
image.png 【CoordinatorLayout|CoordinatorLayout --自定义Behavior 1】4.Textview将跟随Bottom移动了

CoordinatorLayout|CoordinatorLayout --自定义Behavior 1
文章图片
image.png

    推荐阅读