前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发笔记(一百二十七)活用提示窗Toast和Snackbar

Android开发笔记(一百二十七)活用提示窗Toast和Snackbar

作者头像
aqi00
发布2019-01-18 14:54:10
5390
发布2019-01-18 14:54:10
举报
文章被收录于专栏:老欧说安卓老欧说安卓

提示窗Toast

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

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

代码语言:javascript
复制
				Toast toast = new Toast(this);
				View vv = LayoutInflater.from(this).inflate(R.layout.toast_hint, null);
				TextView tv_toast = (TextView) vv.findViewById(R.id.tv_toast);
				tv_toast.setText(text);
				LinearLayout ll_toast = (LinearLayout) vv.findViewById(R.id.ll_toast);
				ll_toast.setBackgroundColor(mBackground);
				toast.setView(vv);
				toast.setGravity(mGravity, 0, 0);
				toast.setDuration(duration);
				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 : 显示提示条。 下面是演示提示条的两个截图,分别展示了滑动取消提示条效果,以及点击交互按钮的界面效果:

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

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_snackbar_simple"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="显示简单提示条"
                android:textColor="@color/black"
                android:textSize="17sp" />

            <Button
                android:id="@+id/btn_snackbar_action"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="显示可交互提示条"
                android:textColor="@color/black"
                android:textSize="17sp" />
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cl_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

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

代码语言:javascript
复制
	public void onClick(View v) {
		if (v.getId() == R.id.btn_snackbar_simple) {
			Snackbar.make(cl_container, "把我往右滑动看看会发生什么事", Snackbar.LENGTH_LONG).show();
		} else if (v.getId() == R.id.btn_snackbar_action) {
			Snackbar.make(cl_container, "这是一个可交互的提示条", Snackbar.LENGTH_LONG)
					.setAction("点我", new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							tv_hint.setText(Utils.getNowTime()+" 您轻轻点了一下Snackbar");
						}
					}).setActionTextColor(Color.YELLOW).show();
		}
	}

点击下载本文用到的自定义提示窗的工程代码 点此查看Android开发笔记的完整目录

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年01月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 提示窗Toast
  • 提示条Snackbar
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档