【问题标题】:How to get widget size after device reboot (For Android 4.4.4)如何在设备重启后获取小部件大小(适用于 Android 4.4.4)
【发布时间】: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 中获取小部件大小?或者,换句话说,我们如何在设备重启后获取小部件的大小?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    这仅适用于果冻豆及以上的设备。

    似乎有一个API可以实现这个目的。

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        Bundle bundle = appWidgetManager.getAppWidgetOptions(appWidgetId);
        heightInDp = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多