安卓的通知组件

常用的两大通知组件:
Notification
Toast
使用的区别:
1.Toast一般使用于程序运行当中,用来提示用户的操作失误
2.notification一般用于程序关闭情况下的后台通知(一般结合广播和服务使用)

Notification的使用步骤:

1.创建notificationManager(通知管理者)对象
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
2.创建notification对象
Notification notification =new Notification(
R.drawable.icon, //图标资源Id
“这是notification”,//通知的文本
System.currentTimeMillis());//系统的当前时间

3.发出通知 即调用notificationManager的方法nitify((第几个通知),通知对象);
notify(1,notification);

Toast的五种使用方式:

1.最常见的使用方式:
Toast. makeText(this, “这是一个toast”, Toast.LENGTH_LONG).show();

2.自定义位置的显示:
Toast toasttoast = Toast.makeText(Context, “自定义位置”, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0); //设置它的重心位置
toast.show();//显示

3.带图片的Toast:
Toast toast=Toast.mmakeText(
Context,
“带图片的Toast”,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 50, -100);//设置位置
LinearLayout layout = (LinearLayout)
toast.getView();//获得Toast这个组件的布局
ImageView image = new ImageView(Context);//新建图片对象
//给图片设置资源
image.setImageResource(R.drawable.wallpaper_tree_small);
layout.addView(image, 0);//给布局添加图片
toast.show();//显示Toast

4.完全自定义的Toast:
LayoutInflater inflater = getLayoutInflater();//膨胀布局对象
View view = inflater.inflate(
R.layout.userdefinedtoast,//自定义的布局
(ViewGroup) findViewById(R.id.toast_layout));//Toast的根布局TextView tvTitle = (TextView)
view.findViewById(R.id.txt_Title);
TextView tvContent = (TextView)
view.findViewById(R.id.txt_content);
ImageView iv = (ImageView)
view.findViewById(R.id.image_toast);
Toast toast = new Toast(Context);//创建Toast对象
toast.setGravity(Gravity.CENTER, 0, 0); //设置位置
toast.setDuration(Toast.LENGTH_LONG);//设置时间
toast.setView(view); //设置布局
toast.show();//显示

5.长时间显示的Toast:
LayoutInflater inflater = getLayoutInflater();//膨胀布局对象
View view = inflater.inflate(
R.layout.userdefinedtoast,//自定义的布局
(ViewGroup) findViewById(R.id.toast_layout));//Toast的根布局TextView tvTitle = (TextView)
view.findViewById(R.id.txt_Title);
TextView tvContent = (TextView)
view.findViewById(R.id.txt_content);
ImageView iv = (ImageView)
view.findViewById(R.id.image_toast);
AlertDialog.Builder b = new AlertDialog.Builder(this);//创建窗口建造者对象
builder.setView(view1);//设置窗口的布局
AlertDialog dialog = builder.create();//创建窗口
dialog.show();//显示窗口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值