Determine the homescreen's appwidgets space gr

2019-05-11 21:34发布

问题:

I have developed a re-sizable app-widget for tablets. The app-widget works nice on most devices with the majority of launchers, however there are some problems with the orientation especially on the GO-HD Launcher in portrait mode. The height of the app-widget is too big and doesn't look nice.

In order to deal with this I have created a method to allow the user to set any extra margins, in the app-widget space, he wants to make it look nice. For example he can set extra 20 pixels top margin when the device is in portrait mode and only 5 pixels when in landscape etc.

Now I am creating an activity to allow users to set the margins. I want to make a small preview area (it will be a linear layout with the same background drawable app-widget has) in order for the user to get an idea of how the app-widget will look like on the home screen with these margins. Actually I have done that but the result is far away from the reality.

So my question is:

Is there any way to get the size (or close to it) of the grid cell that launcher application using to place the app-widgets on the home screen? Please note that I am aware of the fact that not all launchers following the Android formulas for this but what I want is to get a way to calculate an approximation of it.

---- EDIT ----

In order to better understand my problem I have created a screenshot:

First of all the app-widget's background is a shape xml file with no graphics or dimensions at all. It's up to Android to draw it in the available space. In my opinion this is the best GUI design (at least with the background). Now in the screen 1 you can see the widget in portrait mode in the default Android launcher, it looks very nice and it is re-sizing OK. In screen 2 and 3 you can see it in GO-HD launcher (landscape and portrait mode), in this launcher it looks pretty small in landscape mode and very big in portrait mode. I can fix this problem by letting the user adjust the margins as you can see in screen 4.

If you compare the preview (the dark area in the center of screen 4) with the original app-widget in android's default launcher you can see that there is no a good match. What I am asking is how can I present to the user a better preview (closest to the actual app-widget).

Thank you and please forgive my English

回答1:

Have you implemented AppWidgetProvider.onAppWidgetOptionsChanged(Context, AppWidgetManager, int, Bundle)? All well-behaved launchers should report some metrics about your placed widget, which you can retrieve from the bundle, using code like:

int minwidth_dp = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
int maxwidth_dp = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
int minheight_dp = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
int maxheight_dp = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

Note that there is no concept of cell here - these metrics correspond to the actual size of your widget, however many cells it takes up.

The stock launcher maps these to different orientations like so:

minwidth -> portrait width
minheight -> landscape height
maxwidth -> landscape width
maxheight -> portrait height

Any launcher that keeps the same number of rows and columns in both orientations, and that resizes the cells to fit the screen, ought to map these the same way. Be aware, assuming these relationships always hold may produce undesired results if you're embedded in a launcher that does something unusual with its widgets - placing them in only part of the screen, or using a different number of rows or columns in different orientations.

Alternately, some launchers may report the metrics of the current orientation in both min- and max- values, and simply call you again with new metrics when the orientation changes. However, if you are only interested in the current orientation, the behavior of these launchers is compatible with that of the stock launcher.



回答2:

Is there any way to get the size (or close to it) of the grid cell that launcher application using to place the app-widgets on the home screen?

No. Each home screen developer is welcome to do what that developer wishes.

in this launcher it looks pretty small in landscape mode and very big in portrait mode

Then come up with a design that accommodates those sizes. For example, you might look to see what other app widgets do when run on that home screen.



回答3:

I don't know of a way to make a reliable preview, but it might help to use different margins based on orientation. You can use layout-land and layout-port to specify different layouts with different margins.



回答4:

I know it's been awhile, but I was just upvoted on my other answer so I got the chance to glance at this with fresh eyes.

I think the real answer to the original question might be to set the widget's size yourself, or maybe just the height since that seems to be the problem in the screenshots. In your layout set layout_height="@dimen/my_perfect_height" and the launcher will center it within the cell. If you need a little flexibility, you can use a few different layouts with different metrics and let the user choose between them. And of course, you'll have no problem previewing that height since you're the one setting it.