我为 Android 应用程序创建小部件(当然是用 Java)。我有从布局(使用布局 ID)创建的经典 RemoteViews
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.view);
我需要编辑或更改 View (通过 ID 标识)。在经典 View 中很容易,使用 findViewById 函数。
View v = ... //inflate layout R.layout.view
View my = v.findViewById(R.id.myViewId);
processView(my); //filling view
但 RemoteViews 不支持它。可以使用 apply() 获取 View ,但在 processView 和 reapply() 之后我看不到 View 的变化。
View v = rv.apply(context, null);
View my = v.findViewById(R.id.myViewId);
processView(my); //this work's fine
rv.reapply(context,my);
第二个,更糟糕的选择,是从 RemoteViews 获取我需要的 View ,处理它,删除旧 View 并使用 addView() 添加处理过的新 View 。
RemoteViews rv = ...
View my = ... //apply, find and process
//remove old view
RemoteViews rvMy = ... //create RemoteViews from View
rv.addView(rvMy)
但我不知道如何从 View 创建 RemoteView(这可能吗?)。有什么想法可以解决这个问题吗?
最佳答案
试试这个方法:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
remoteViews.setTextViewText(R.id.widget_textview, text); <---- here you can set text
// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
这是一篇有助于理解小部件行为的帖子:
关于java - 如何从 RemoteView 中的布局编辑/更改 View 或从 View 创建 RemoteView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19884218/