Android layout with square buttons

前端 未结 6 752
感动是毒
感动是毒 2020-11-30 09:31

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

6条回答
  •  爱一瞬间的悲伤
    2020-11-30 09:53

    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.

提交回复
热议问题