【Android】通知的实现


基本实现前面已经记录过了,这里直接从自定义通知开始。

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风格 需要自定义通知,声明在一个类中可以反复调用
			}
		});



直接执行就可以看到自定义通知 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值