有人可以帮我这么做吗?我的代码是这样的:
public CustomClass extends View {
//uses ondraw() to do something
}
Run Code Online (Sandbox Code Playgroud)
为了在主屏幕上显示我的自定义视图,我创建了一个扩展广播接收器的类:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但添加ViewGroup到RemoteView引用不起作用...上面的main.xml布局仅包含LinearLayout.我想为它添加一个自定义视图对象.但是在运行之后,屏幕上没有任何显示......
请帮我这样做.提前致谢.