Android: AppWidget with custom view not working

前端 未结 4 974
余生分开走
余生分开走 2020-11-29 02:43

I am creating a appwidget that consists of a single custom view called Foo.

xml/widget.xml:



        
相关标签:
4条回答
  • 2020-11-29 02:58

    I pretty much left my custom view intact, and implemented an ImageView for my widget, then rendered the view using the getDrawingCache()

    MyView myView = new MyView(context);
    myView.measure(150,150);
    myView.layout(0,0,150,150);
    myView.setDrawingCacheEnabled(true);
    Bitmap bitmap=myView.getDrawingCache();
    remoteViews.setImageViewBitmap(R.id.dial, bitmap);
    
    0 讨论(0)
  • 2020-11-29 02:58

    You cannot have ANY custom view used in widget. In fact, even those android-predfined views are not all supported.

    For detailed list of supported widgets/layouts, please read the documentation. Anything other than those documented cannot be placed in widgets.

    0 讨论(0)
  • 2020-11-29 03:08

    Another way to do this without using getDrawingCache() :

    MyView myView = new MyView(this);
    myView.measure(500, 500);
    myView.layout(0, 0, 500, 500);
    Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
    myView.draw(new Canvas(bitmap));
    remoteViews.setImageViewBitmap(R.id.imageView, bitmap);
    

    I used cache not to redraw all the view so I couldn't use the code above. And I find it more elegant. I hope it could be useful to someone.

    0 讨论(0)
  • 2020-11-29 03:13

    see the documentation.

    AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar and TextView are the supported views. For layouts you have to use FrameLayout, LinearLayout or RelativeLayout.

    0 讨论(0)
提交回复
热议问题