Converting RemoteView into a View

2019-02-06 00:42发布

I am trying to render a RemoteViews instance onto a Canvas, like I do with a regular View. I use

RemoteViews.apply(context, null)

and it returns a FrameLayout with all the views nested and properly measured (location and size is correct,) but after using .draw on the returned view, it renders all elements with no values -- TextViews are empty, AnalogClock is reset at 00:00 and so on.

Any ideas? I'm lost :(

1条回答
走好不送
2楼-- · 2019-02-06 01:24

Not sure if the question is still actual. Nevertheless here is my experience with RemoveViews. It appears you cannot just call draw() on the returned view. You have to add this view to a parent container to make it a part of global view hierarchy. For instance, you have an Activity with a single FrameLayout in it. Your code will look like this.

FrameLayout parent = findViewById(R.id.container);
View view = RemoteViews.apply(getActivity(), parent);
parent.addView(view);

Now you should be able to see tests. If you set listeners, they will work properly too.

查看更多
登录 后发表回答