Android 5.x support.design.widget---Snackbar
2016-07-14 12:30
387 查看
Snackbar
android.support.design中的控件
↳ android.support.design.widget.Snackbar
Snackbars 简述
1 提供操作时的轻量级反馈,他们在屏幕的底部显示一个简短的信息在移动设备
2 Snackbar出现在屏幕上的其他控件上,同一时间只能显示一个
3 SnackBar会在超时或是用户交互后自动消失
4 加强版的Toast,轻量级的Dialog
两个重要的方法
setAction(CharSequence, android.view.View.OnClickListener). 通过上面方法可以添加一个点击事件响应 Snackbar.Callback via setCallback(Callback). 在SnackBar显示和消失时候,通过上面方法可以进行消失和显示的消息回调
Constants |
---|
int LENGTH_INDEFINITE Show the Snackbar indefinitely. |
int LENGTH_LONGE Show the Snackbar for a long period of time. |
int LENGTH_SHORT Show the Snackbar for a short period of time |
Mothod |
---|
void dismiss() |
Dismiss the Snackbar |
int getDuration() |
Return the duration. |
View getView() |
Returns the Snackbar’s view. |
boolean isShown() |
Return whether this Snackbar is currently being shown. |
boolean isShownOrQueued() |
Returns whether this Snackbar is currently being shown, or is queued to be shown next. |
Static Snackbar make(View view, CharSequence text, int duration) |
Make a Snackbar to display a message |
Snackbar will try and find a parent view to hold Snackbar’s view from the value given to view. |
Static Snackbar make(View view, int resId, int duration) |
Make a Snackbar to display a message. |
Snackbar setAction(int resId, View.OnClickListener listener) |
Set the action to be displayed in this Snackbar. |
Snackbar setAction(CharSequence text, View.OnClickListener listener) |
Set the action to be displayed in this Snackbar. |
Snackbar setActionTextColor(ColorStateList colors) |
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener). |
Snackbar setActionTextColor(int color) |
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener). |
Snackbar setCallback(Snackbar.Callback callback) |
Set a callback to be called when this the visibility of this Snackbar changes. |
Snackbar setDuration(int duration) |
Set how long to show the view for. |
Snackbar setText(int resId) |
Update the text in this Snackbar. |
Snackbar setText(CharSequence message) |
Update the text in this Snackbar. |
void show() |
Show the Snackbar |
一、SnackBar与Toast的差别
Snackbar.make(container, "最基本的SnackBar", Snackbar.LENGTH_SHORT).show(); Toast.makeText(context, "Toast显示", Toast.LENGTH_SHORT).show()
两者的区别
二者都有三个参数,区别就在于第一个参数,其中Toast传入的是Context,而Snackbar传入的是View。
Toast
系统级窗口,相当于悬浮在所有View的上面。
SnackBar
它需要有一个View来承载,SnackBar会遍历整个View Tree来找到一个合适的View承载SnackBar的View,
PS:想要实现动画交互效果,需要传入CoordinatorLayout对象, 传入RelativeLayout或者LinearLayout无动画效果的
二、SnackBar的使用
1.1 添加相应的依赖库
compile ‘com.android.support:design:24.0.0’1.2 实现布局
snackbar显示时有动效<android.support.design.widget.CoordinatorLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentBottom="true"> </android.support.design.widget.CoordinatorLayout>
1.3 Activity中实现
PS:Activity 必须是AppComatActivity 的子类否则报错(Error inflating class android.support.design.widget.Snackbar$SnackbarLayout)
Snackbar.make(container, "最基本的SnackBar", Snackbar.LENGTH_SHORT).show();
三、SnackBar扩展
3.1 带有显示隐藏监听的SnackBar
Snackbar snackbar = Snackbar.make(container, "我是带有显示隐藏监听的SnackBar", Snackbar.LENGTH_SHORT); snackbar.setCallback(new Snackbar.Callback() { @Override public void onDismissed(Snackbar snackbar, int event) { super.onDismissed(snackbar, event); Toast.makeText(SnackBarActivity.this, "SnackBar隐藏",Toast.LENGTH_SHORT).show(); } @Override public void onShown(Snackbar snackbar) { super.onShown(snackbar); Toast.makeText(SnackBarActivity.this, "SnackBar显示", Toast.LENGTH_SHORT).show(); } }); snackbar.show();
3.2 自定义背景和字体的snackBar
Snackbar snackbar = Snackbar.make(container, "我是自定背景色和字体颜色的SnackBar",Snackbar.LENGTH_SHORT); //设置背景色 snackbar.getView().setBackgroundColor(getResources().getColor(R.color.cheng1)); ((TextView)snackbar.getView().findViewById(R.id.snackbar_text)).setTextColor(getResources().getColor(R.color.bluelight1)); snackbar.show();
3.3 带有点击事件的SnackBar
Snackbar snackbar = Snackbar.make(container, "我是带有点击事件的SnackBar", Snackbar.LENGTH_SHORT); //设置背景色 snackbar.getView().setBackgroundColor(getResources().getColor(R.color.cheng1)); ((TextView)snackbar.getView().findViewById(R.id.snackbar_text)).setTextColor(getResources().getColor(R.color.graylight2)); snackbar.setAction("显示HAHA", new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(SnackBarActivity.this,"HAHA",Toast.LENGTH_SHORT).show(); } }); snackbar.setActionTextColor(getResources().getColor(R.color.white)); snackbar.show();
3.4 带有ICON的SnackBar
Snackbar snackbar = Snackbar.make(container, "我是带有ICON的SnackBar", Snackbar.LENGTH_SHORT); //设置背景色 snackbar.getView().setBackgroundColor(getResources().getColor(R.color.cheng1)); ( (TextView)snackbar.getView().findViewById(R.id.snackbar_text)).setTextColor(getResources().getColor(R.color.graylight2)); snackbar.setActionTextColor(getResources().getColor(R.color.white)); snackbar.setAction("显示HAHA", new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(SnackBarActivity.this,"HAHA",Toast.LENGTH_SHORT).show(); } }); ImageView icon=new ImageView(SnackBarActivity.this); icon.setImageResource(R.mipmap.icon_position_o); //icon插入布局 Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout)snackbar.getView(); snackbarLayout.setGravity(Gravity.CENTER_VERTICAL); snackbarLayout.addView(icon,0); snackbar.show();
四 、demo
项目样例地址
https://github.com/BlueAmberTec/DesignWidget
相关文章推荐
- Android MVC
- android developer tiny share-20160714
- android developer tiny share-20160714
- Android自定义View制作动态炫酷按钮实例解析
- Android 编程小问题及Android studio小技巧
- Android 登录Web 时对cookie 处理
- Android提高第十九篇之"多方向"抽屉
- 用mac Android Studio在开源中国(OSChina)上完成代码版本控制
- Android提高十八篇之自定义Menu(TabMenu)
- Android Studio使用git无法pull或commit的解决方法
- Android数据存储五种方式总结
- android 获取屏幕高度和宽度 的方法
- Android提高十七篇之多级树形菜单的实现
- Android提高十六篇之使用NDK把彩图转换灰度图
- 解决Android Studio编译后安装apk报错:The APK file does not exist on disk
- Android提高第十五篇之ListView自适应实现表格
- Android提高第十四篇之探秘TelephonyManager
- Android提高第十三篇之探秘蓝牙隐藏API
- android 获取url 参数值
- Android 开发中,有哪些坑需要注意?