Android 小部件:addView 无法与 RemoteViews 对象一起使用

发布于 2025-01-05 17:59:32 字数 512 浏览 0 评论 0原文

我想在运行时将文本视图添加到我的小部件中。我看到很多教程,其中 RemoteViews 对象调用 addView 方法将视图添加到某些布局。

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
TextView t = new TextView(context);
views.addView(R.id.view_container, t);
appWidgetManager.updateAppWidget(thisWidget, views);

但是 addView 方法显示此错误: The method addView(int, RemoteViews) is undefined for the type RemoteViews.True, this method is Define for the ViewGroup class,但为什么它对每个人都有效?有没有办法在运行时将文本视图添加到某些 RemoteViews 中?

I want to add textviews to my widget on runtime. I saw many tutorials where RemoteViews object calls addView method to add a view to some layout.

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
TextView t = new TextView(context);
views.addView(R.id.view_container, t);
appWidgetManager.updateAppWidget(thisWidget, views);

But addView method shows this error: The method addView(int, RemoteViews) is undefined for the type RemoteViews.True, this method is defined for the ViewGroup class, but why is it working for everyone then? And is there a way to add textviews to some RemoteViews on runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2025-01-12 17:59:32

签名不是 <代码>RemoteViews.addView(int, RemoteViews)

您似乎有一个 TextView 作为第二个参数。

像这样的东西应该有效:

RemoteViews remoteViewToAdd = new RemoteViews(context.getPackageName(), LAYOUT_ID);
...
views.addView(R.id.view_container, remoteViewToAdd);

Isn't the signature RemoteViews.addView(int, RemoteViews)?

You seem to have a TextView as the second parameter.

Something like this should work:

RemoteViews remoteViewToAdd = new RemoteViews(context.getPackageName(), LAYOUT_ID);
...
views.addView(R.id.view_container, remoteViewToAdd);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文