我想移动小吃店和工厂一起移动,就像默认行为一样,但默认情况下,只有当我们点击工厂时才会发生,工厂为snackbar
和工厂本身向上移动,
但希望它发生在其他事件发生时,如布局上其他按钮不喜欢自己制造,如何在布局点击其他按钮时触发晶圆厂向上移动并为snackbar
腾出位置
谢谢
按下按钮:
public void onClick(View view) {
if(spinner.getText().length() <= 0){
showListMenu(spinner);
}else{
showSnack("Added to bag we will hold it for 1 hour!", view.getRootView());
}
}
里面的showSnack:
Snackbar.make(view.getRootView(), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
final Animation myAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.bounce);
double animationDuration = 2 * 1000;
myAnim.setDuration((long)animationDuration);
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.20, 20.00);
myAnim.setInterpolator(interpolator);
fab.startAnimation(myAnim);
只需用这样的小吃吧锚定fab:
Snackbar.make(view.view.findViewById(R.id.fab), getResources().getString(R.string.fab_msg), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
使用足够的CoordinatorLayout
,无需在Java代码上进行任何更改。在CoordinatorLayout
中添加按钮
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
对于这两个点击事件,SnackBar
和Fab
顺利上升
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});