Android 小部件:如何在运行时将视图添加到 RemoteViews
我想在运行时将以线性布局排列的文本视图添加到我的小部件中。我执行以下操作:
LinearLayout l = new LinearLayout(context);
for (int i = 0; i < 10; i++) {
TextView t = new TextView(context);
t.setText("Hello");
l.addView(t); }
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
RemoteViews view = new RemoteViews (context.getPackageName(), l.getId());
views.addView(R.layout.main, view);
但是当我添加小部件时,我收到加载小部件问题错误。似乎 RemoteViews
在接收构造的视图 id 作为参数时存在问题。但我无法引用 XML 资源,因为它们是在运行时创建的。在运行时用 TextViews
填充 RemoteViews
的正确方法是什么?
I want to add text views arranged in a linear layout to my widget on runtime. I do following:
LinearLayout l = new LinearLayout(context);
for (int i = 0; i < 10; i++) {
TextView t = new TextView(context);
t.setText("Hello");
l.addView(t); }
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
RemoteViews view = new RemoteViews (context.getPackageName(), l.getId());
views.addView(R.layout.main, view);
But when I add widget I get a Problem loading widget error. Seems like RemoteViews
has a problem with receiving a constructed view id as a parameter. But I can't make a reference to XML resource, because they are created on runtime. What is the proper way to populate the RemoteViews
with TextViews
on runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RemoteView 中只能使用 xml 资源。在运行时创建的视图应基于预定义的 xml 视图。
Only xml resources can be used in RemoteViews. Views created on runtime should be based on predefined xml views.