2013-01-11 49 views
24

Android的設計模式指南說widget的內容和佈局可以動態調整,以用戶通過調整操作這裏定義的尺寸:設計指南中提供Design guide for widgets將widget的內容和佈局動態調整爲用戶通過調整大小定義的大小。安卓

例子: Example image provided in the design guide.

,但我沒有看到文檔中有關如何完成此操作的任何內容。我們如何根據調整大小操作更改佈局?任何想法的方法將不勝感激。

+0

任何輸入任何人? – Gogu

+1

由於AppWidgetProvider#onAppWidgetOptionsChanged()方法,這似乎是一個Jellybean +功能。 [One](http://code4reference.com/2012/07/android-homescreen-widget-with-alarmmanager/)例子我發現實現它。 –

+1

有沒有辦法知道小部件的尺寸,以便我們可以渲染適當的視圖? Jellybean +也很好。 – Gogu

回答

22

感謝A - C,這是可能的軟糖和以上設備,並且很容易實現。 下面是一個使用onAppWidgetOptionsChanged方法示例代碼

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
@Override 
public void onAppWidgetOptionsChanged(Context context, 
     AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { 

    Log.d(DEBUG_TAG, "Changed dimensions"); 

    // See the dimensions and 
    Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId); 

    // Get min width and height. 
    int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); 
    int minHeight = options 
      .getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); 

      // Obtain appropriate widget and update it. 
    appWidgetManager.updateAppWidget(appWidgetId, 
      getRemoteViews(context, minWidth, minHeight)); 

    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, 
      newOptions); 
} 

/** 
* Determine appropriate view based on width provided. 
* 
* @param minWidth 
* @param minHeight 
* @return 
*/ 
private RemoteViews getRemoteViews(Context context, int minWidth, 
     int minHeight) { 
    // First find out rows and columns based on width provided. 
    int rows = getCellsForSize(minHeight); 
    int columns = getCellsForSize(minWidth); 

    if (columns == 4) { 
     // Get 4 column widget remote view and return 
    } else { 
        // Get appropriate remote view. 
     return new RemoteViews(context.getPackageName(), 
       R.layout.quick_add_widget_3_1); 
    } 
} 

/** 
* Returns number of cells needed for given size of the widget. 
* 
* @param size Widget size in dp. 
* @return Size in number of cells. 
*/ 
private static int getCellsForSize(int size) { 
    int n = 2; 
    while (70 * n - 30 < size) { 
    ++n; 
    } 
    return n - 1; 
} 
+0

首先,你不應該僅限於4個單元格。使用這樣的循環:http://pastebin.com/reexPt1m。其次,你忘了計算屏幕密度。第三,沒有保證,那個小部件大小爲264dp將佔用4個單元。我的一臺索尼設備適合3個電池使用264dp。您應該使用dp中的佈局寬度而不是單元格。 – Pijusn

+0

@Pius更新了獲取單元格的代碼。謝謝。 True不應僅限於4個單元格。雖然不能保證它會佔用4個單元,但根據您的佈局進行佈局是一個很好的估計。 – Gogu

+0

Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);與返回的Bundle相同newOptions – zest

0

@Choletski @azendh

改變佈局後的一些單擊事件不叫了

我通過創建功能解決了這個問題那setOnClickPendingIntent 在視圖上,然後返回它。

例如代碼如下

private RemoteViews getConfiguredView (RemoteViews remoteViews, Context context){ 

    Intent refreshIntent = new Intent(context, EarningsWidget.class); 
    refreshIntent.setAction(REFRESH_ACTION); 
    PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 3, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    remoteViews.setOnClickPendingIntent(R.id.refreshButton, toastPendingIntent); 
    return remoteViews; 
} 

而不是函數被調用,你做「獲取相應的遠程視圖」。

return getConfiguredView(new RemoteViews(context.getPackageName(), R.layout.activity_widget), context);