2013-01-11 47 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);