[英]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.