以编程方式将 TextViews 添加到主屏幕小部件

Adding TextViews to home screen widget programmatically(以编程方式将 TextViews 添加到主屏幕小部件)

本文介绍了以编程方式将 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 添加到主屏幕小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:以编程方式将 TextViews 添加到主屏幕小部件

基础教程推荐