2011-03-25 56 views
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。我想爲它添加一個自定義視圖對象。但運行後,屏幕上沒有任何顯示...

請幫我這麼做。提前致謝。

回答

7

無法將自定義View添加到應用程序窗口小部件。請參閱App Widgets Dev Guide的the "Creating the App Widget Layout" section,瞭解允許哪些類型的View

Android 3.0增加了對某些視圖的支持以顯示集合。有關詳細信息,請參閱「使用應用程序窗口小部件和集合」部分。

否則,動態添加允許的View到應用的Widget,充氣RemoteViews並獲得對它的引用後,您可以使用它addView(View)方法,或addView(View)方法在任何View對象已經在RemoteViews的。

相關問題