Lately I have been playing a lot with Flex 4. There are so many new great features and I hope to talk about them more in the future. Today I want to talk about layouts and more specifically creating your own custom layouts. Layouts are now separated from the components themselves which not only makes the components lighter but gives us more granular control over our layout. We can control the layout of our application Or for a container
When using layouts in Flex 4 you will basically be working with the 4 built in layouts.
- BasicLayout
- HorizontalLayout
- VerticalLayout
- TileLayout
Now that we have the shell of our new layout we can talk about what it takes to actually create the layout. In the updateDisplayListMethod the first thing we need to do is figure out how many children we are working with. The LayoutBase class that we extended exposes a property called target. With this we can figure out the number of children. In my example we will be scattering images on a page so imagine the children as images. Next we are going to loop through each of these children and do a number of things. First we get the actual element that we are working with and set the bounds for the element. By using NaN as size arguments we are telling the method to use its preferred size. Next I am getting the size of the element or in my case the image size. The reason for this pretty important to our layout. We don't want the x or y position to ever cut off our image so if say the layout bounds size (the max x+y bounds) for total size minus the size of our children then we will never have them running off the screen.
Finally I have a convinent method for grabbing a random number between 0 and the max bounds. Now I can go back to my main application component and do something like this.
If you run the program you will get something that looks like this. You can click on the image to see a live demo. If you refresh the page you will notice a new random position is calculated for each of the children.
As you can see Flex 4 layouts are incredibly easy to build even for a newbie like myself. Here is the final code for the RandomLayout Class.

#1 by Dennis on 8/19/10 - 4:41 PM
I was looking for a random image loader that i can use in flex thats how i surft to you blog.
Container thats loads the images add random for the folder
assets\ image1 +1