如何解决动态将自定义视图添加到RemoteViews
| 有什么可以帮助我做到这一点的吗?我的代码是这样的: public CustomClass extends View {
//uses ondraw() to do something
}
为了在主屏幕上显示我的自定义视图,我创建了一个类来扩展Broadcast Receiver:
public class customAppWidgetProvider extends BroadcastReceiver {
public void onReceive(Context context,Intent intent) {
String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.main);
//Here I want to create my custom view class object and I want to add this view to linear layout in main.xml
CustomClass object = new CustomClass(context) ;
LinearLayout layout = new LinearLayout(context) ;
layout.setLayoutParameters(new LayoutParameters(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(object);
views.addview(R.id.linearlayout,(ViewGroup) layout) ;
views.setOnClickPendingIntent(R.id.analog_appwidget,PendingIntent.getActivity(context,new Intent(context,AlarmClock.class),PendingIntent.FLAG_CANCEL_CURRENT));
int[] appWidgetIds = intent.getIntArrayExtra(
AppWidgetManager.EXTRA_APPWIDGET_IDS);
AppWidgetManager gm = AppWidgetManager.getInstance(context);
gm.updateAppWidget(appWidgetIds,views);
}
}
}
但是,向RemoteView参考中添加“ 2”是行不通的...上面的main.xml布局仅包含LinearLayout。我想向其中添加一个自定义视图对象。但是运行此命令后,屏幕上没有任何显示...
请帮助我做到这一点。提前致谢。
解决方法
无法将自定义“ 3”添加到应用小部件。请参阅App Widgets开发指南的\“创建App Widget布局\”一节以了解允许的
View
类型。
Android 3.0添加了对某些视图的支持以显示集合。有关详细信息,请参见“将应用程序小部件与集合一起使用”一节。
否则,要在允许View
膨胀并获得对其的引用之后向App Widget动态添加允许的View
,可以对RemoteViews
中已有的任何View
对象使用addView(View)
方法或addView(View)
方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。