基本实现前面已经记录过了,这里直接从自定义通知开始。
1.写一个布局文件:
新建一个dialog的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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DAAA"
android:orientation="horizontal"
android:padding="8dp" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>
这个就是我们自定义的通知
2.触发通知按钮的回调函数如下:
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = new Toast(MainActivity.this);
//加载自定义布局
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);
ImageView imageView = (ImageView)view.findViewById(R.id.image);
imageView.setImageResource(R.drawable.ic_launcher);
TextView textView = (TextView)view.findViewById(R.id.text);
textView.setText("自定义土司通知");
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(view);
toast.show();
//统一UI风格 需要自定义通知,声明在一个类中可以反复调用
}
});
直接执行就可以看到自定义通知