2010-08-19 63 views
0

我有一個顯示一組信息的Widget。我想要做的是讓用戶有機會選擇背景顏色/圖像。我想在用戶選擇小部件選擇背景時彈出一個窗口。基於用戶輸入爲Widget添加自定義背景

那麼我會如何彈出?

如何動態應用背景?

謝謝任何​​幫助,你可以給!

回答

0

我認爲,如果你通過點擊小工具打開PreferenceActivity可能會更好。 然後選擇合適的圖片,並通過使用獲得相應的首選項:

String background = prefs.getString("background", "bgpink"); // get the Prefs! 
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.widget); 

if (background.equals("bgpink")) { 
    views.setImageViewResource(R.id.widgetLayout, R.drawable.bgpink); // set the background 
} 

的代碼的setBackground不起作用。我閱讀了「setImageViewResource」的文檔,這是我認爲的一個開放性問題。讓我知道你是否找到了設置背景的解決方案。

0

我還沒有具體做過,但我會去了解它通過以下方式:

  • 如果您正在尋找拿起那些已經在系統上的文件,火的意圖來接(以列表視圖
    • 創建一個對話框)(圖像視圖/文本視圖(複合繪項目和一些文字的)選擇:ACTION_PICK),如果你提供的是選項提供正確的Intent.data元素
    • 背景
    • 存儲值返回到一個首選項文件
    • 從喜好閱讀和應用背景

希望這有助於

+0

我看到這是如何提示一種方式來提示用戶,但我不明白如何實際實現背景更改。 – 2010-08-19 21:25:20

+0

我已經找到了android:configure選項。謝謝:-) – 2010-08-20 05:31:32

+0

@atolar,如果你能詳細說明,那將會很棒。我爲其中一位將有興趣瞭解更多有關這些選項的信息。 – 2010-08-20 13:15:49

0

我最終什麼了使用:

public static void setWidgetBackground(Context context, AppWidgetManager appWidgetManager, int mAppWidgetId, int newBackground){ 
      // theLayout is a global int in my case 

      switch (newBackground){ 
      case R.id.brown: 
       theLayout = R.layout.brown_widget; 
       break; 
     case R.id.darkblue: 
      theLayout = R.layout.darkblue_widget; 
       break; 
      case R.id.darkpurple: 
       theLayout = R.layout.darkpurple_widget; 
       break; 
      // ... etc. 
      } 


      // save the settings so we can load and apply again next time the widget is updated 
      WidgetConfigure.saveLayout(context,mAppWidgetId,theLayout); 

      // call the actual update so that the right background is applied 
      this.updateAppWidget(context, appWidgetManager, mAppWidgetId, theLayout); 

    } 

希望這是幫助他人。