[英]Android AppWidget: Add Custom Views to Widget Layout
我想在我的窗口小部件中显示一些TextView和EditView的列表。
我使用以下代码生成TextView和EditView的列表
public LinearLayout getMainBodyLayout(List<Item> data) {
LinearLayout mainLL = new LinearLayout(context);
mainLL.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < data.size(); i++) {
Item ritem = data.get(i);
LinearLayout item = new LinearLayout(context);
TextView name = new TextView(context);
EditText nos = new EditText(context);
name.setText(ritem.getName());
nos.setText(ritem.getNo());
item.addView(name);
item.addView(nos);
mainLL.addView(item);
}
return mainLL;
}
public void updateWidget(LinearLayout ll) {
AppWidgetManager manager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, MainWidget.class);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_my);
remoteViews.setTextViewText(R.id.title, title);
remoteViews.setTextViewText(R.id.widget_error, errorMsg);
// I ve to add ll to this remoteViews
manager.updateAppWidget(thisWidget, remoteViews);
}
我必须将返回的LinearLayout添加到我的小部件Linearlayout中。 请提供给我最好的方法。
尝试在XMl中制作Layout并在Java文件中设置其值...并使用方法
RemoteViews.setTextviewText(id,string);
当我在小部件中使用它时,请尝试此方法
private class ABC extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
public ABC(Context context, final AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, WidgetAnimate.class);
}
public void run() {
String k= XMLfunctions.data1();
remoteViews.setTextViewText(R.id.tv12,k);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
为您提供完整的代码将更大。 我给您一些代码示例,您可以根据需要对其进行优化。
这也是一个很好的教程
如果您想优化列表,可以查看本教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.