I myself assign a height to it by content, but I want to know its height in dp or pixels, so that when changing content, leave the height the same as it was.

 FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) fr.getLayoutParams(); params.height = -2; fr.setLayoutParams(params); 

And another question: if I assign

 params.height = 100; 

What is this value measured in px or dp ?

  • one
    > What is this value measured in px or dp? px - gcoder
  • even if I make a measurement in onResume returns by zeros @Override public void onResume () {super.onResume (); int topYcoord = fr.getTop (); int bottomYcoord = fr.getBottom (); Log.i ("height", topYcoord + "" + bottomYcoord); fr.measure (View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); Log.i ("height", fr.getMeasuredHeight () + "" + fr.getMeasuredWidth ()); } - J Mas
  • @ YuriySPb did it, assigned the pixels manually, but it still returns by zeros. - J Mas
  • @eldqs, updated the answer. - Yuriy SPb
  • one
    @eldqs, as far as I know, you can stuff a fragment in any subclass of ViewGroup. - Yuriy SPb

1 answer 1

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:

  1. FrameLayout returns 0 always if there are no children in it.
  2. If there is a child, it returns its height, even if it (the child) does not fully occupy it.
  3. With RelativeLayout everything works.

Conclusion: Use RelativeLayout.

  • Thanks, that is necessary. - J Mas
  • @YuriySPb, unfortunately they are the top, that the bottom return at 0, what can be done? - J Mas
  • You 're welcome ) - YuriySPb
  • @YuriSPb, actually the problem still remains, int topYcoord = fr.getTop (); int bottomYcoord = fr.getBottom (); they return by zeros - J Mas
  • Zeros returns, if I'm not mistaken, if I use this before onResume () activates. I will update the answer. - Yuriy SPb