本文介绍了以编程方式将TextViews添加到主屏幕小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式将文本视图"控件添加到我的主屏幕小部件.在下面的示例中,我用TextViews填充Linearlayout,但是在这里我应该如何使用RemoteViews?它仅接受xml资源布局作为参数.
I want to programmatically add Text Views controls to my home screen widget. In the following example I populate Linearlayout with TextViews, but how should I use RemoteViews here? It only accepts xml resource layout as a parameter.
public class MyWidget extends AppWidgetProvider {
public void onUpdate(Context _context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
LinearLayout l = new LinearLayout(_context);
for (int i = 0; i < 10; i++) {
TextView t = new TextView(_context);
t.setText("Hello");
l.addView(t);
}
}
}
我看到的所有教程都使用其预定义控件的值显式填充RemoteViews对象.我想以编程方式添加控件.
All tutorials I saw explicitly populate RemoteViews object with values for its predefined controls. And I want to add controls programmaticaly.
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.my_widget);
views.setTextViewText(R.id.widget_control1, value1);
views.setTextViewText(R.id.widget_control2, value2);
推荐答案
好,appwidgets不可能.仅接受xml资源.
Ok, It is impossible for appwidgets. Only xml resources are accepted.
这篇关于以编程方式将TextViews添加到主屏幕小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!