您的位置:首页 > 其它

安卓的通知组件

2016-05-25 09:31 218 查看
常用的两大通知组件:

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();//显示窗口
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: