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.
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.
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
I have a RelativeLayout and i want it to fill the whole width of the view, but if it would become bigger than xx dip it should stay in this size.
I tried to use android:maxWidth="xxdp" but it is not working with match_parent.
Rather than setting width to match_parent, set the width to "0dp" and the weight of the layout to 1.0f.
Assuming there are no other views in the same parent, this should force the RelativeLayout to fill the width. maxWidth should now work like you expected.
I have a nested TableLayout (TableLayout->TableRow->TableLayout->TableRow). The inner TableLayouts have their width set to MATCH_PARENT. It is not expanding to match its parent. See attached screenshots.
EDIT:
I can tweak the width using android:weight and setting the android:layout_width="0dp". I still want to understand this behaviour.