动态添加一个自定义的视图RemoteViews

任何可能做到这一点?我的代码是这样的:

 public CustomClass extends View {
//uses ondraw() to do something
}

在屏幕上显示我的自定义视图我创建了一个类来扩展广播接收器:

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, 0,
      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);
  }
 }
}
posted @ 2017-03-31 22:03  AndroidGemini  阅读(489)  评论(0编辑  收藏  举报