在Android中动态地将控件放入通知中

在Android中动态地将控件放入通知中,android,Android,与本主题一样: 我也想做同样的事情,但我想动态地创建我的视图。我无法访问任何xml,也无法编写relativelayout/imageview xml 我是通过Android版Firefox上的JNI来做这件事的,所以我还学到了很多东西。此时,我只能从Java移植到JNI。我想知道你是否可以告诉我如何做与原始主题相同,但动态。这就是原始主题的作用: public class MainActivity extends Activity { @SuppressWarnings("depre

与本主题一样:

我也想做同样的事情,但我想动态地创建我的视图。我无法访问任何xml,也无法编写relativelayout/imageview xml

我是通过Android版Firefox上的JNI来做这件事的,所以我还学到了很多东西。此时,我只能从Java移植到JNI。我想知道你是否可以告诉我如何做与原始主题相同,但动态。这就是原始主题的作用:

public class MainActivity extends Activity {

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, "Custom Notification", when);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        mNotificationManager.notify(1, notification);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
我也想做同样的事情,但我想动态地创建我的视图。我无法访问任何xml,也无法编写relativelayout/imageview xml

严格地说,你不能做你想做的事。RemoteView仅通过布局资源文件定义其中的小部件和容器。Java方法允许您操作这些小部件和容器的选择属性,但在没有布局资源文件的情况下无法添加小部件和容器

欢迎您为不同种类的小部件和容器创建存根布局资源文件,然后使用RemoteView构造函数为这些小部件和容器创建RemoteView,然后使用RemoteView上的AddView将它们缝合到任意结构中。这将是麻烦的,仍然可能不允许你实现你想要的