我想在运行时将以线性布局排列的 TextView 添加到我的小部件。我执行以下操作:
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
在接收构造的 View ID 作为参数时出现问题。但是我不能引用 XML 资源,因为它们是在运行时创建的。在运行时使用 TextViews
填充 RemoteViews
的正确方法是什么?
最佳答案
在 RemoteViews 中只能使用 xml 资源。在运行时创建的 View 应基于预定义的 xml View 。
关于安卓小工具 : How to add Views to RemoteViews on runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9297418/