Gau*_*ham 27 android android-widget
Android设计模式指南说小部件的内容和布局可以动态调整到用户通过调整大小操作定义的大小:小部件的设计指南
设计指南中提供的示例:

但我在文档中没有看到如何实现这一目标.我们如何根据调整大小操作更改布局?任何有关该方法的想法将不胜感激.
Gau*_*ham 24
感谢A - C,这对于Jellybean及以上设备是可行的,并且易于实现.下面是使用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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11082 次 |
| 最近记录: |