【问题标题】:How to add view programmatically to notification (add to RemoveViews) when using custom notification使用自定义通知时如何以编程方式将视图添加到通知(添加到 RemoveViews)
【发布时间】:2019-07-03 11:36:13
【问题描述】:

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

我想要这个

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

【问题讨论】:

    标签: android android-layout layout notifications


    【解决方案1】:

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

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

    【讨论】:

    • 这不是我的答案,我想以编程方式将视图添加到 remoteViews
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多