RemoteView addView 不起作用
我有一个应用程序小部件,我想将视图(TextView
等)添加到 RemoteView
,但它从未显示。
代码如下:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_row_layout);
newView.setTextViewText(R.id.textUser, "1234");
views.addView(views.getLayoutId(), newView);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
有什么想法吗?
这就是我最终所做的:
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_row_layout);
newView.setTextViewText(R.id.textUser, "1234");
ComponentName thisWidget = new ComponentName(this,WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, newView);
I have an app widget and I'd like to add Views (TextView
, etc.,) to the RemoteView
but it never shows up.
Here goes the code:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_row_layout);
newView.setTextViewText(R.id.textUser, "1234");
views.addView(views.getLayoutId(), newView);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
Any ideas?
This is what I ended up doing:
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_row_layout);
newView.setTextViewText(R.id.textUser, "1234");
ComponentName thisWidget = new ComponentName(this,WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, newView);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
addView() 方法需要您想要添加此新视图的布局内视图的 id,而不是布局本身。
而不是这个:
试试这个:
假设你的布局看起来像这样:
文件:layout/widget_layout.xml
The addView() method needs the id of the view inside the layout you want to add this new view to, not the layout itself.
Instead of this:
Try this:
Assuming your layout looks something like this:
file: layout/widget_layout.xml