动态调整窗口小部件的内容和布局的大小用户通过调整大小来定义。安卓 [英] Dynamically adjusting widget's content and layout to the size the user defined through resize. Android
问题描述
机器人设计图案指南认为小部件的内容和布局可以动态调整到用户通过调整操作在此定义的尺寸:设计指南部件
Android design pattern guide says widget's content and layout can be dynamically adjusted to the size the user defined through resize operation here: Design guide for widgets
例:
Example provided in the design guide:
不过,我没有看到任何的文档至于如何做到这一点。我们如何改变布局按大小调整操作?任何有关该方法的思想将AP preciated。
But I do not see anything in the docs as to how to accomplish this. How do we change the layout as per resize operation? Any ideas regarding the approach will be appreciated.
推荐答案
由于A - C,这是可能的杰利贝恩及以上的设备,是很容易实现。
下面是使用示例code onAppWidgetOptionsChanged
方法
Thanks to A--C , this is possible for Jellybean and above devices and is simple to implement.
Below is the sample code using onAppWidgetOptionsChanged
method
@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;
}
这篇关于动态调整窗口小部件的内容和布局的大小用户通过调整大小来定义。安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!