Reputation: 436
I want to create a custom diagonal layout like androids predefined linear layout..so i started with a class extending ViewGroup and overrided OnLayout and OnMeasure methods..However I am finding it difficult to understand where to write the logic for alligning all the Views such that they will be placed diagonally
I read few blogs on it How to Create Custom Layout in Android by Extending ViewGroup Class
Also followed the google I/O video But still not clear about how to get started with creating a diagonal layout..can someone suggest any additional resources in this regard..???
Upvotes: 0
Views: 427
Reputation: 496
The logic to measure children should be in onMeasure
. In here, you want the sum of all children height to be as big as the height given in the heightMeasureSpec
. Same goes for the width.
To do so, you can divide the width and height from measureSpec by the number of children, and call the child views measure
method with those computed values.
The logic to position the children is in onLayout
. In here, you call the child views layout
method one after another, passing it positions (left, top, ...) incremented after layouting each child.
You must take care of child views margins in both methods, as well as your container padding.
Lucas Rocha has a very good article (with lot of reverse engineering) for custom views.
Upvotes: 1