标签:otto lib .com stat int 基础 xmlns ansi with
Snackbar
是Android Support Design Library
库支持的一个控件,用于在界面下面提示一些关键信息,跟Toast不同的地方是SnackBar允许用户向右滑动消除它,同时,也允许在SnackBar中设定一个Action,当用户点击了SnackBar里面的按钮的时候,可以进行一些操作,所以,功能绝对是很强大的。
官方推荐使用CoordinatorLayout(
另一个Android Support Design Library
库支持的控件)容纳。因为使用这个控件,可以保证Snackbar
可以让用户通过向右滑动退出。
在xml文件中定义snackBar的容器:Coordinator容器:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:onClick="createSnackbar" android:text="Snackbar" android:layout_width="match_parent" android:layout_height="wrap_content"/> <android.support.design.widget.CoordinatorLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> </RelativeLayout>
activity中显示 Snackbar(它的用法与Toast很像,它出现后三秒之后从底部弹出,或者用户 向右滑动,它被滑出)
public class SnackbarTest extends AppCompatActivity { CoordinatorLayout container; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_snackbar_test); container = (CoordinatorLayout) findViewById(R.id.container); } public void createSnackbar(View v){ Snackbar.make(container,"Snackbar maker", Snackbar.LENGTH_LONG).show(); } }
Snackbar的第一个容器参数也可以是 LinearLayout或其他容器,当Snackbar弹出时,没有向右滑出效果。
Snackbar.make(container,"Snackbar with button",Snackbar.LENGTH_LONG)
.setAction("action", new View.OnClickListener() {
@Override
public void onClick(View v) {
//
}
})
.setActionTextColor(Color.RED)
.show();
自定义 Snackbar.Callback 类
public class MyCallback extends Snackbar.Callback{ @Override public void onDismissed(Snackbar transientBottomBar, int event) { super.onDismissed(transientBottomBar, event); if(event == DISMISS_EVENT_SWIPE || event == DISMISS_EVENT_TIMEOUT || event == DISMISS_EVENT_CONSECUTIVE){ } } @Override public void onShown(Snackbar sb) { super.onShown(sb); // } }
Snackbar设置callBack()
Snackbar.make(container,"Snackbar maker", Snackbar.LENGTH_LONG) .setCallback(new MyCallback()) .show();
补充Snackbar消失的类型:
/** Indicates that the Snackbar was dismissed via a swipe.*/ public static final int DISMISS_EVENT_SWIPE = 0; /** Indicates that the Snackbar was dismissed via an action click.*/ public static final int DISMISS_EVENT_ACTION = 1; /** Indicates that the Snackbar was dismissed via a timeout.*/ public static final int DISMISS_EVENT_TIMEOUT = 2; /** Indicates that the Snackbar was dismissed via a call to {@link #dismiss()}.*/ public static final int DISMISS_EVENT_MANUAL = 3; /** Indicates that the Snackbar was dismissed from a new Snackbar being shown.*/ public static final int DISMISS_EVENT_CONSECUTIVE = 4;
后续查看补充:
http://www.jianshu.com/p/cd1e80e64311
Coordinator 容器:
http://www.jianshu.com/p/488283f74e69
http://www.cnblogs.com/itgungnir/p/6210803.html
安卓Android Support Design Library——Snackbar
标签:otto lib .com stat int 基础 xmlns ansi with
原文地址:http://www.cnblogs.com/NeilZhang/p/7417682.html