Get the size of my homescreen widget

前端 未结 6 2074
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 15:46

I just want to know how big my current widget is. I found tons of questions to set the minimum size, but I don\'t want to set it. Instead I want to show that informations which

6条回答
  •  無奈伤痛
    2021-02-02 16:15

    You can add an method to your widget provider and get your widget sizes when you resize it for all versions of Android started from API 16:

        @Override
        public void onAppWidgetOptionsChanged (Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle widgetInfo) {
    
            int width = widgetInfo.getInt (AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
            int height = widgetInfo.getInt (AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
    
        }
    

    And as for new added widget you can get its wizes by getting its sizes from its layout and multiply it to its cols num:

        AppWidgetProviderInfo widgetInfo = AppWidgetManager.getInstance (context).getAppWidgetInfo (widgetId);
    
        private int getColsNum (int size) {
            return (int) Math.floor ((size - 30) / 70);
        }
    
        public int getWidthColsNum () {
            return getColsNum (widgetInfo.minWidth);
        }
    
        public int getHeightColsNum () {
            return getColsNum (widgetInfo.minHeight);
        }
    
        int width = widgetInfo.minWidth * getWidthColsNum ();
        int height = widgetInfo.minHeight * getHeightColsNum ();
    

    Getting widget cols num was taken from official Android guidelines.

提交回复
热议问题