public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Hello Snackbar", Snackbar.LENGTH_LONG).show(); } }); } }
实现按钮随下面的FloatingActionButton一起向上动画,所以自定义一个
CoordinatorLayout.Behavior如下:
public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> { public FloatingActionButtonBehavior(Context context, AttributeSet attrs) { } @Override public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) { return dependency instanceof SnackbarLayout; } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { Log.i("0000", dependency.getTranslationY() + "getTranslationy"); float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight()); Log.i("0000", dependency.getHeight() + "getheight"); Log.i("0000", translationY + "translationy"); child.setTranslationY(translationY); return true; }}
在布局中配置<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.getbase.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_margin="16dp" <!--layout_behavior="自定义实现的Behavior全包名"--> app:layout_behavior="com.getbase.coordinatorlayoutdemo.FloatingActionButtonBehavior" app:fab_icon="@drawable/ic_done" /> </android.support.design.widget.CoordinatorLayout>
自定义实现CoordinatorLayout.Behavior
最新推荐文章于 2022-12-21 21:33:13 发布