As far as I've understood Android's View rendering process, the root Layout will start the measurement and ask each of its children for it's size. If one of them is a Viewgroup itself, it will again ask the children, which either just return their set size, or calculate it if they're set to wrap_content.
Now how does that work with match_parent? The view can't calculate it's size since it has to know the parent's size first, right? So the parent would have to calculate it's own size and then send that to the child, which then can go on with it's own layout.
Does that make match_parent a little more inefficient than wrap_content or even fixed dp-values?
Actually, match_parent says that "Hi parent, I want to be as big as your left available space".For example, if ViewGroup Parent has two children: child A at index 0 with wrap_content and child B at index 1 with match_parent. And A says he wants to be 100px wide, then the width of B will be : total width of Parent - padding of Parent - margin of A - 100px - margin of B
Related
I have an ImageView and I want to set something like this:
min_with=match_parent and max_width=value
does someone know how to do that?
I am guessing your parent is not the root parent and your parent width is not match_parent either.
You can set your parent width as wrap_content and set child width as wrap_content but set child min width to some dp so your child won’t have less width than min width but it will hug the parent on all sides.
You can also take advantage of linear view and its weightSum, weight to childrens
We can go in details if you share more data and small drawing of what you want to do.
These days I am learning how to customize the view on Android.I know if we set the minimum width, then the system will compare the min width we set and the measured width and choose the proper one to fit the view.What I am confusing is that how does android system change the size of the view according to the content of the view as the word "wrap_content" means.I want to know more details about how to achieve "wrap_content". Thanks a lot.
This is what Official Documentation says (I am adding just a part of it for a quick read)
When a View object's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View object's descendants. A View object's measured width and measured height values must respect the constraints imposed by the View object's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent View may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (that is, if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass).
The measure pass uses two classes to communicate dimensions. The ViewGroup.LayoutParams class is used by View objects to tell their parents how they want to be measured and positioned. The base ViewGroup.LayoutParams class just describes how big the View wants to be for both width and height. For each dimension, it can specify one of:
MATCH_PARENT, which means the View wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the View wants to be just big enough to enclose its content (plus padding).
MeasureSpec objects are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:
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.
EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.
In my Android app I want to change the height of a RelativeLayout in an animation. The layout contains a hierarchy of children, which are all the same size as the parent.
If I change the height of the parent view by adjusting its LayoutParams, the children keep their sizes. Changing the inner child's size also doesn't change the parent's size, and changing the size of all of the View at the same time also doesn't seem to work.
How do I increase the height of several nested views at the same time?
In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.
I understand that MATCH_PARENT means that the View wants to be as big as its parent (minus padding), and WRAP_CONTENT means that the View wants to be just big enough to enclose its content (plus padding)
My question is, what happens when a MATCH_PARENT view (view A) is put inside a WRAP_CONTENT view (view B)? How are the parameters of the two views A and B calculated in this case?
It fills all the space possible allowed for its parents, looking up on the hierarchy.
If the parent of the WRAP_CONTENT view (View B) has a size limit, it will take that size.
If no hierarchical parent has size (all are wrap_content until the root view) it will take up the whole screen.