4
任何幫助我這樣做?我的代碼是這樣的:動態添加自定義視圖到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);
}
}
}
但添加ViewGroup
到遠程視窗參考無法工作...上面的main.xml佈局只包含LinearLayout。我想爲它添加一個自定義視圖對象。但運行後,屏幕上沒有任何顯示...
請幫我這麼做。提前致謝。