美文网首页
CoordinatorLayout --自定义Behavior

CoordinatorLayout --自定义Behavior

作者: wangxiaojin | 来源:发表于2018-08-20 10:40 被阅读0次

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


image.png

2.自定义 CoordinatorLayout.Behavior
public class EasyBehavior extends CoordinatorLayout.Behavior<TextView> {//这里的泛型是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(注意包名要写对)


image.png

4.Textview将跟随Bottom移动了


image.png

相关文章

网友评论

      本文标题:CoordinatorLayout --自定义Behavior

      本文链接:https://www.haomeiwen.com/subject/wljfiftx.html