Android之Notification实现
Android之Notification实现
在我们的相应程序运行的时候为了不打断当前程序的运行,我们经常会使用Notification来告知用户有新来电或新的短信。
下面先介绍一下toast的简单提醒:
- private void baseToast(){
- Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT).show();
- }
第一个参数是得到上下文,第二个是提醒的具体内容,第三个是提醒的时间。
接下来看一下如何自定义一个Toast提醒:
- //自定义toast
- private void customToast(){
- //得到inflater对象和view
- LayoutInflater inflater=getLayoutInflater();
- View layout=inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
- //得到view下的相应的控件
- ImageView image = (ImageView) layout.findViewById(R.id.image);
- image.setImageResource(R.drawable.ic_launcher);
- TextView text = (TextView) layout.findViewById(R.id.text);
- text.setText("Hello! This is a custom toast!");
- //设置toast
- Toast toast=new Toast(getApplicationContext());
- toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
- toast.setDuration(Toast.LENGTH_SHORT);
- toast.setView(layout);
- toast.show();
- }
在layout 下定义了toast_layout.xml布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:id="@+id/toast_layout_root"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp"
- android:background="#DAAA"
- >
- <ImageView android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginRight="10dp"
- />
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:textColor="#FFF"
- />
- </LinearLayout>
运行后效果:
|
评论暂时关闭