The code you provided in the question will calmly return to
params.height
element height.
The problem may arise if it is initially
= "wrap_content" || "match_parent"; //вернёт -2 || -1, а не высоту в пикселях
Then a few options. For example, you can get the coordinates of the "top" and "bottom" of the element:
FrameLayout fr = //находим элемент int topYcoord = fr.getTop(); int bottomYcoord = fr.getBottom();
And from them calculate the height of the element:
int calculatedHeight = bottomYcoord-topYcoord;
And apply to the element:
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) fr.getLayoutParams(); params.height = calculatedHeight; fr.setLayoutParams(params);
UPD 0:
All of the above works in onResume() when the system has calculated all the coordinates of all the layouts.
Before onResume () you can, for example:
//int h = fr.getMeasuredHeight(); вернёт 0, т.к. не измерялось ещё. //вроде как принудительно измеряет элемент //"0" здесь - это какие-то константы. Что и как они конкретно делают - магия для меня fr.measure(0,0); //теперь высота измерена и вернет !0 int h = fr.getMeasuredHeight();
UPD 1:
Experimented. Findings:
- FrameLayout returns 0 always if there are no children in it.
- If there is a child, it returns its height, even if it (the child) does not fully occupy it.
- With RelativeLayout everything works.
Conclusion: Use RelativeLayout.