使用自定义通知时,如何以编程方式添加通知(添加到删除视图)



我想以编程方式添加远程视图。RemoteViews具有诸如SetText等视图等视图的方法,找不到将视图添加到RemoteViews

的方法

我想要这个

RemoteViews contentView = new 
RemoteViews(BaseApplication.getContext().getPackageName(), 
R.layout.view_notification);
contentView.addView(new TextView(getContext()));

您可以使用这样的自定义视图

Create Notification_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:padding="10dp" >
<ImageView
    android:src="@mipmap/ic_launcher"
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="10dp" />
<TextView
    android:textSize="13dp"
    android:textColor="#000"
    android:text="Testing"
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/icon"
    />
<TextView
    android:textSize="13dp"
    android:textColor="#000"
    android:text="Testing is awecome"
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/icon"
    android:layout_below="@id/title"
     />
</RelativeLayout>

和代码

RemoteViews customview = new RemoteViews(getPackageName(), R.layout.custom_push);
customview.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
customview.setTextViewText(R.id.title, "Notification title");
customview.setTextViewText(R.id.dex, "Notification Description");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon).setContent(customview);
Notification notification = mBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);

最新更新