【发布时间】:2014-09-16 05:55:54
【问题描述】:
我有一个小部件,它的布局应该根据当前小部件的大小而改变。我使用下面的代码来处理这样的需求。
public void onAppWidgetOptionsChanged(Context context,
AppWidgetManager appWidgetManager,
int appWidgetId,
Bundle newOptions) {
int heightInDp = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
int heightInCell = getCellSize(heightInDp);
RemoteViews remoteViews = org.yccheok.jstock.gui.widget.Utils.getRemoteViews(appWidgetId, getWidgetWatchlistFragmentResourceId(context, appWidgetId));
if (heightInCell >= 3) {
remoteViews.setViewVisibility(R.id.header, View.VISIBLE);
} else {
remoteViews.setViewVisibility(R.id.header, View.GONE);
}
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
这很好用。直到我意识到在 Android 4.4.4 中,当您关闭并打开设备时,只会触发onUpdate。
但是,在 4.1.2 中,onAppWidgetOptionsChanged 将被触发。在这种情况下,我更容易根据小部件大小处理 UI 布局。与onAppWidgetOptionsChanged 一样,我们收到了Bundle newOptions。从捆绑包中,我们可以查询当前小部件的大小。
但是,我们如何在onUpdate 中获取小部件大小?或者,换句话说,我们如何在设备重启后获取小部件的大小?
【问题讨论】: