Change LinearLayout height to 0 programmatically

I will change LinearLayout from its original height to 0 using

 ViewGroup.LayoutParams params = getLayoutParams(); params.height = newHeight; requestLayout(); 

Everything works, except newHeight = 0 - the layout height returns to its original height. How can i avoid this?

Setting visibility to GONE if newHeight == 0 does not help.

+4
source share
5 answers

Try it.....

  LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId); LinearLayout.LayoutParams lp = (LayoutParams) layout.getLayoutParams(); lp.height = 0; 
+9
source

No need to set params height using setBaseAttributes () ? int height = ... will not affect params ...

0
source

Are you sure that you get the object of the actual parameters, and not a copy of it? I would try the following code:

 ViewGroup.LayoutParams params = getLayoutParams(); params.height = newHeight; setLayoutParams(params); requestLayout(); 
0
source

Try setting a new params layout object. This should work

 ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,0); layout.setLayoutParams(lp); 
0
source
 layout = (LinearLayout)findViewById(R.id.linearTop); layout.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; ViewGroup.LayoutParams params = layout.getLayoutParams(); params.height = ViewGroup.LayoutParams.WRAP_CONTENT; layout.setLayoutParams(params); 
0
source

Source: https://habr.com/ru/post/1432042/


All Articles