Android: View by id? (from Widget Provider)
I know only one way to get a view by id:
getViewById(R.drawable.imageButton)
But how am I supposed to get this imageButton if i don't have an activity (developing a widget application)
I am able to get a context. I suppose it is enough 开发者_Python百科to get the view - context contains only one widget isn't it? And if not - then how to get each widget views ?
For Widgets you have to work with RemoteViews, for example
RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.widget);
Here you can find a tutorial of how to use them
Inside onReceive
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
int[] appWidgetIds = mgr.getAppWidgetIds(new ComponentName(context, YourWidgetProvider.class));
RemoteViews rv = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
rv.setImageViewResource(R.id.yourImageView, R.drawable.yourBitmap);
mgr.updateAppWidget(appWidgetIds, rv);
There is no way you can get handle of ImageView. But you can use setImageViewBitmap or setImageViewResource or setImageViewUri from RemoteView class to set the bitmap of ImageView. http://developer.android.com/reference/android/widget/RemoteViews.html
精彩评论