Possible to define widget's behaviour/layout for resize and orientation change in 3rd party launchers?

為{幸葍}努か 提交于 2019-12-20 15:31:24

问题


I can't seem to find anything on the following problem with a widget I want to code: Apart from the resize-ability introduced in Android 3.1, I want my widget to be resizable in custom launchers like LauncherPro or ADWLauncher. Is there a way I can define how my widget changes its layout once it's resized? So, for example, button A is shown on the left for 2x1 but is shown on te right for 3x3. Also I want it to change its appearance/layout when due to screen rotation the widget size changes (because e.g. 3x1 has different height and width in landscape and portrait).

Any hints would be helpful. Thanks a lot! Till


回答1:


Is there a way I can define how my widget changes its layout once it's resized?

Your app widget doesn't change its layout once it's resized.

So, for example, button A is shown on the left for 2x1 but is shown on te right for 3x3.

That is not the purpose of resizeable app widgets. Resizing is for the use of ListView, GridView, etc., that will absorb the additional scrollable space.

Also I want it to change its appearance/layout when due to screen rotation the widget size changes (because e.g. 3x1 has different height and width in landscape and portrait).

For that, you should be able to use standard orientation resource sets (e.g., res/layout/ and res/layout-land/).




回答2:


As of Android 4.1 it is possible to use onAppWidgetOptionsChanged() to find out the size of a widget on resize and change the layout. So in your AppWidgetProvider:

@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle bundle) {
        int minWidth = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
        int maxWidth = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
        int minHeight = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
        int maxHeight = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
        RemoteViews rv = null;
        //Size of a 2x1 widget on a Nexus 4 (other screen densities will differ)
        if(minWidth == 152 && maxWidth == 196 && minHeight == 58 && maxHeight == 84){
            rv = new RemoteViews(context.getPackageName(), R.layout.widget2x1);
        } else {
            rv = new RemoteViews(context.getPackageName(), R.layout.widget);
        }
        ...
        /* Set some stuff in your layout here */
        ...
        appWidgetManager.updateAppWidget(appWidgetId, rv);
}

The trouble is you need to find the min/max width and height for each screen density/size in order to know when the widget is resized a certain amount to warrant a layout change.



来源:https://stackoverflow.com/questions/6461870/possible-to-define-widgets-behaviour-layout-for-resize-and-orientation-change-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!