Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What unit of measure does LayoutParams use?

Tags:

pI am working with a linear layout and want to set the maximum height of the view. Under "normal" circumstances, I want the view to use "wrap_content." However, occasionally the circumstances may push the layout to an undesirable size. When this happens, I want to limit the height to a maximum 300dp.

I have set the size of the view using the following when the list in the layout exceeds 4 list items:

LinearLayout listLayout = (LinearLayout) dialog.findViewById(R.id.listLayout); if(list.size() > 4){     LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300);                                listLayout.setLayoutParams(params); } 

Reviewing the documentation leaves me with no clue as to the unit of measure that is applied. What are the units of measure in this situation (dp, sp, px, ...)?

Running tests, even setting the value to 100 has the list exceeding desired height.

Please advise

like image 701
Roy Hinkley Avatar asked Jun 29 '13 18:06

Roy Hinkley


People also ask

What is LayoutParams?

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports. The base LayoutParams class just describes how big the view wants to be for both width and height.

What does view Measure () do?

UNSPECIFIED : This is used by a parent to determine the desired dimension of a child View . For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child View wants to be given a width of 240 pixels.

What does Wrap_content mean in Android?

Wrap Content: Wrap content takes the length of the text and wraps its content. Example: “This is a Wrap Text”. The wrap content starts wrapping from “This” and ends at “text”.

What is Match_parent and Wrap_content in Android?

Conclusion : fill_parent and match_parent are the same, used when we want the height or width of a view to be as big as its parent view, fill_parent being deprecated. wrap_content is used when we want the view to occupy only as much space as required by it. You may also read : Android UI Layouts.


1 Answers

According to the documentation you linked: pixels. See this function

the width, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels

Even though the function you're using doesn't have any explicit documentation, it is implied that it uses the same documentation as the function with the most parameters. The function itself probably looks like:

LinearLayout.LayoutParams(int width, int height) {     this(width, height, /*some default value*/); } 

i.e. it's simply calling the 3-parameter version with a default value.

like image 85
J David Smith Avatar answered Oct 13 '22 16:10

J David Smith