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

时间:2019-07-03 11:36:13

标签: android android-layout layout notifications

我想以编程方式将视图添加到remoteViews。 remoteViews具有自己的用于setText等视图的方法,找不到用于将视图添加到remoteViews的方法

我想要这个

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

1 个答案:

答案 0 :(得分:0)

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

创建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);