The GridLayout
API docs is very difficult to learn ......
is there any one who can teach me how to set child View
s to have something like "weight" of LinearLayout
?
Right now it looks like all are placed on the left hand side,
I have tried many times and still can not make it like each one the half width of the screen.
Edited : I don't know what can I do for when the childen are all wrap_content
......
even if I want to set some image in specific size , this class helps me to make ImageView
wrap_content
.........it cannot operate normal , did I miss some setting ?!?
Note: The information below the horizontal line is no longer accurate with the introduction of Android 'Lollipop' 5, as
GridLayout
does accommodate the principle of weights since API level 21.Quoted from the Javadoc:
It's also worth noting that
android.support.v7.widget.GridLayout
contains the same information. Unfortunately it doesn't mention which version of the support library it was introduced with, but the commit that adds the functionality can be tracked back to July 2014. In November 2014, improvements in weight calculation and a bug was fixed.To be safe, make sure to import the latest version of the gridlayout-v7 library.
The principle of 'weights', as you're describing it, does not exist with
GridLayout
. This limitation is clearly mentioned in the documentation; excerpt below. That being said, there are some possibilities to use 'gravity' for excess space distribution. I suggest you have read through the linked documentation.For an example and some practical pointers, take a look at last year's blog post introducing the
GridLayout
widget.Edit: I don't think there's an xml-based approach to scaling the tiles like in the Google Play app to 'squares' or 'rectangles' twice the length of those squares. However, it is certainly possible if you build your layout programmatically. All you really need to know in order two accomplish that is the device's screen dimensions.
Below a (very!) quick 'n dirty approximation of the tiled layout in the Google Play app.
The result will look somewhat like this (on my Galaxy Nexus):
Do you know View.getViewTreeObserver().addOnGlobalLayoutListener()
By this you can calculate the sizes.
I achieve your UI effect by GridView:
Starting in API 21 the notion of weight was added to GridLayout.
To support older android devices, you can use the GridLayout from the v7 support library.
The following XML gives an example of how you can use weights to fill the screen width.
If you use fragments you can prepare XML layout and than stratch critical elements programmatically
This layout for 3 equal columns. First element takes 2x2 Result in the picture
For other peeps: If you have to use GridLayout due to project requirements then use it but I would suggest trying out TableLayout as it seems much easier to work with and achieves a similar result.
Docs: https://developer.android.com/reference/android/widget/TableLayout.html
Example:
Just a quick follow up and note that it is possible now to use the support library with weighted spacing in GridLayout to achieve what you want, see: