自定义Notification

 可以通过Notification.Builder.setContent方法自定义Notification。setContent方法的原型如下:
public Builder setContent(RemoteViews views);
 RemoteViews 是一种视图容器,只是这种视图容器支持的视图有限,目前RemoteViews 只支持布局和控件:
 布局:
FrameLayout、LinearLayout、RelativeLayout
 控件:
AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView

 RemoteViews类构造方法的原型如下:
public RemoteViews(String packageName, int layout);
      packageName:包名
      layout与RemoteViews对象关联的布局资源ID
public class NotificationActivity extends Activity {

    private NotificationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     * 显示通知
     * @param view
     */
    public void showNotification(View view){
        //创建RemoteViews对象
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
        //设置布局中的TextView显示的文本
        remoteViews.setTextViewText(R.id.tv001,"更新自定义文本内容");
        //创建Builder对象
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.person).setContent(remoteViews);
        //显示自定义的Notification
        manager.notify(1,builder.build());
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/tv001"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:gravity="center"
        android:textSize="20dp"
        android:text="自定义内容"/>

    <ImageView
        android:id="@+id/iv001"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/person" />
</LinearLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉辰56

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值