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.
Related
I'm struggling with getting LinearLayout to behave like I want it. Unfortunately, both dimension modes MATCH_PARENT and WRAP_CONTENT don't seem to fit for my purposes.
Here's why: I want the child that is added to the LinearLayout to be completely visible. Nothing should be cut off. So normally, I should use WRAP_CONTENT to achieve this behaviour.
But, if there's more space in the LinearLayout than the child really needs, I also want it to fill that space. This is of course what MATCH_PARENT is for.
However, I can't use MATCH_PARENT because in case there is less space in the LinearLayout than my child needs, using MATCH_PARENT will cut off the child which I don't want.
So this leaves me somewhat puzzled as to how I can achieve what I want: How can I allow a child to fill additional space in the LinearLayout (if available) while at the same time forcing the LinearLayout to be at least as big as the child needs in order to be completely visible?
Put your child view inside a ScrollView with width MATCH_PARENT, height MATCH_PARENTand set both the child's dimensions MATCH_PARENT
Instead of using MATCH_PARENT for your child, use WRAP_CONTENT and set your child's weight to 1,So it will take all the empty space in your LinearLayout. Suppose your LinearLayout's height is 128dp, and your child 's height is 56dp, your child will be 128dp . If you set WRAP_CONTENT on your LinearLayout, it will be 56dp, and it child will still take all the place it needs.
If you want your LinearLayout to have a minimum width or height, and not match_parent, you can do setMinimumWidth() or setMinimumHeight() in your xml layout or in your code programmatycally depending your childs default height and width.
I want to keep distance from left and right, but keep width of the widget flexible. How can I define it in Android?
I do not understand your question, but i guess you're looking for a way to give your ListView a width which is dynamic based on e.g. the screen width.
You should have a look at MATCH_PARENT
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. For each dimension, it can specify one
of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which
means that 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)
an exact number
There are subclasses of LayoutParams for different subclasses of ViewGroup. For
example, AbsoluteLayout has its own subclass of LayoutParams which
adds an X and Y value.
https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
https://developer.android.com/guide/topics/ui/declaring-layout.html
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.
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