How to create a regular, resizable grid without nested weights?

拟墨画扇 提交于 2019-12-12 01:39:22

问题


I've one of the simplest layouts imaginable: A num pad.

I want to create a fragment containing a 3 x 4 grid of buttons. The layout should automatically resize the num pad to fill the available space.

I've learned, that GridLayout is not up to the task, and TableLayout/TableRow or nesting LinearLayouts means nesting weights, which is also discouraged for performance reasons. A RelativeLayout won't work either, because that requires at least one button with given dimensions.

So, is there a clean way to create a regular grid that will resize to fill its parent?

Any help is appreciated, thx!


回答1:


You will need a custom compound control.

Check the following link:

http://developer.android.com/guide/topics/ui/custom-components.html#compound

Make the control fill the available space. Make it to have 12 buttons. Calculate the size and position of them based on their position and the available space.

Depending on your needs you might also need to override onMeasure() and onLayout() defined earlier in the above document, in the "Fully Customized Components" section.



来源:https://stackoverflow.com/questions/17023577/how-to-create-a-regular-resizable-grid-without-nested-weights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!