I want to make a layout similar to this one:
www.ImageBanana.net - layout.png http://www.imagebanana.com/img/9kmlhy66/thumb/layout.png
Four square buttons on
I'm usually doing things like that:
View v; // the view i want to layout squared
v.measure(1,1);
int size = v.getMeasuredHeight(); // or v.getMeasuredWidth()...
LayoutParams params = ... // either your own, or returned from View
params.width = size;
params.height = size;
v.setLayoutParams(params);
This is certainly not the most sophisticated code, as it leaves most capabilities that can be exploited in MeasureSpec, but it gets the job done, as 'v.measure(1,1)' just says "Measure my view as it is!".
The absolute plus in my approach is that you don't have to subclass anything and you don't have to add some OnGlobalLayoutListener to your ViewTreeObserver. This works all at the spot where you build or inflate your layout in code.