动态添加视图到Android窗口小部件

问题描述:

我programmically在运行时创建一个视图,我想这个观点是在运行时动态添加到我的LinearLayout。这里是code我:

I am programmically creating a view at runtime and I want to this view to be added to my linearlayout dynamically at runtime. Here is the code I got:

public class Widget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        RemoteViews updateViews = new RemoteViews(context.getPackageName(),
                R.layout.main);

        DemoView dv = new DemoView(context);

            // Stuck here...How do I add my new dv View to my android widget

        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }


     private class DemoView extends View {
        public DemoView(Context context) {
            super(context);


        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            Paint LedColor = new Paint();
            Paint BlankColor = new Paint();
            Path p,p1;
            Matrix m = new Matrix();

            LedColor.setStyle(Paint.Style.FILL);
            LedColor.setColor(0xffffffff);

            BlankColor.setStyle(Paint.Style.FILL);
            BlankColor.setColor(0xff111111);

            DigitPaths dp = new DigitPaths();

            p = dp.GetDigitPath(-1);
            p1 = dp.GetDigitPath(5);

            p.offset(50, 50);
            p1.offset(50, 50);

            m.setScale(6.5f, 6.5f);
            p.transform(m);
            p1.transform(m);
            canvas.drawPath(p,BlankColor);
            canvas.drawPath(p1, LedColor);
        }
    }
}

任何帮助将是AP preciated!

Any help would be appreciated!

这是不可能到自定义查看添加到应用程序部件。请参见的创建应用程序窗口小部件布局应用的部分小工具开发人员指南什么查看类型都是允许的。

It's not possible to add a custom View to an app widget. See the "Creating the App Widget Layout" section of the App Widgets Dev Guide for what View types are allowed.

Android 3.0的添加了一些看法,以显示集合的支持。请参见使用应用程序的窗口小部件与集合一节。

Android 3.0 adds support for some views to display collections. See the "Using App Widgets with Collections" section for details.

否则,要动态地允许的查看添加到应用程序的Widget,充气后的 RemoteViews ,并得到一个参考它,你可以使用它的 addView(视图)方法,或 addView(视图)方法上的任何查看对象已经在 RemoteViews

Otherwise, to dynamically add an allowed View to an App Widget, after inflating the RemoteViews and getting a reference to it, you can use its addView(View) method, or the addView(View) method on any of the View objects already in the RemoteViews.