Android 活用提示窗Toast和Snackbar
发布日期:2021-11-12 07:57:33 浏览次数:28 分类:技术文章

本文共 3094 字,大约阅读时间需要 10 分钟。

原文地址:http://blog.csdn.net/aqi00/article/details/54342019

提示窗Toast

大家平时都经常用Toast,可是你是否发现,系统默认的Toast样式太过单调乏味呢?其实Toast的界面也允许开发者自行定制,只要定义好提示窗的布局文件,即可调用Toast类的setView方法设置自定义窗口画面。包括背景、对齐方式、窗口内部控件等等界面元素,均可由你自己定制。

下面是自定义提示窗的两个截图,分别展示了不同背景与不同对齐方式下的界面效果:


下面是自定义提示窗的代码例子:

[java]   
  1. Toast toast = new Toast(this);  
  2. View vv = LayoutInflater.from(this).inflate(R.layout.toast_hint, null);  
  3. TextView tv_toast = (TextView) vv.findViewById(R.id.tv_toast);  
  4. tv_toast.setText(text);  
  5. LinearLayout ll_toast = (LinearLayout) vv.findViewById(R.id.ll_toast);  
  6. ll_toast.setBackgroundColor(mBackground);  
  7. toast.setView(vv);  
  8. toast.setGravity(mGravity, 00);  
  9. toast.setDuration(duration);  
  10. toast.show();  



提示条Snackbar

Snackbar是Android Support Design Library库的一个新控件,与Toast相比,Snackbar不仅仅用来提示消息,还允许进行交互,从而改善了用户体验。

使用Snackbar需要导入android-support-design,同时design库依赖于android-support-v7-appcompat,所以design库与appcompat库要同时导入到工程中。另外,Snackbar最好配合控件CoordinatorLayout使用,因为这样Snackbar才能够像通知那样通过右滑手势取消。

Snackbar的用法与Toast类似,常用方法说明如下:

make : 构造一个Snackbar对象。可指定提示条的上级视图、提示消息文本、显示时长等信息。

setText : 设置提示消息的文本内容。

setAction : 设置交互按钮的文本与点击监听器。

setActionTextColor : 设置交互按钮的文本颜色。

setDuration : 设置提示消息的显示时长。

show : 显示提示条。


下面是演示提示条的两个截图,分别展示了滑动取消提示条效果,以及点击交互按钮的界面效果:

  


下面是演示用的布局文件内容:

[html]   
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:padding="5dp" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:orientation="vertical" >  
  10.   
  11.         <LinearLayout  
  12.             android:layout_width="match_parent"  
  13.             android:layout_height="wrap_content"  
  14.             android:orientation="horizontal" >  
  15.   
  16.             <Button  
  17.                 android:id="@+id/btn_snackbar_simple"  
  18.                 android:layout_width="0dp"  
  19.                 android:layout_height="wrap_content"  
  20.                 android:layout_weight="1"  
  21.                 android:gravity="center"  
  22.                 android:text="显示简单提示条"  
  23.                 android:textColor="@color/black"  
  24.                 android:textSize="17sp" />  
  25.   
  26.             <Button  
  27.                 android:id="@+id/btn_snackbar_action"  
  28.                 android:layout_width="0dp"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:layout_weight="1"  
  31.                 android:gravity="center"  
  32.                 android:text="显示可交互提示条"  
  33.                 android:textColor="@color/black"  
  34.                 android:textSize="17sp" />  
  35.         </LinearLayout>  
  36.     </LinearLayout>  
  37.   
  38.     <TextView  
  39.         android:id="@+id/tv_hint"  
  40.         android:layout_width="match_parent"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_alignParentBottom="true"  
  43.         android:layout_marginBottom="30dp"  
  44.         android:textColor="@color/black"  
  45.         android:textSize="17sp" />  
  46.   
  47.     <android.support.design.widget.CoordinatorLayout  
  48.         android:id="@+id/cl_container"  
  49.         android:layout_width="match_parent"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_alignParentBottom="true" />  
  52.   
  53. </RelativeLayout>  

下面是演示用的代码例子片段:

[java]   
  1. public void onClick(View v) {  
  2.     if (v.getId() == R.id.btn_snackbar_simple) {  
  3.         Snackbar.make(cl_container, "把我往右滑动看看会发生什么事", Snackbar.LENGTH_LONG).show();  
  4.     } else if (v.getId() == R.id.btn_snackbar_action) {  
  5.         Snackbar.make(cl_container, "这是一个可交互的提示条", Snackbar.LENGTH_LONG)  
  6.                 .setAction("点我"new View.OnClickListener() {  
  7.                     @Override  
  8.                     public void onClick(View v) {  
  9.                         tv_hint.setText(Utils.getNowTime()+" 您轻轻点了一下Snackbar");  
  10.                     }  
  11.                 }).setActionTextColor(Color.YELLOW).show();  
  12.     }  
  13. }  

转载地址:https://blog.csdn.net/happy_love1990/article/details/76599259 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android:控件AutoCompleteTextView 自动提示
下一篇:Android 屏幕分辨率

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月08日 20时50分38秒