It's just freaking me out, but I can't make my layout depend on weight. There are two relative layouts and two linear layouts and some elements in both of these layouts. I've put android:layout_weight="0.2 to the first one, android:layout_weight="0.1 to the second one, android:layout_weight="0.5 to the third one, and android:layout_weight="0.2 to the last one. The sum is 1, right? But it doesn't work. I'm afraid, it's because of different layout_height and maybe different categories of layouts? The XML-code is here
My experience with the layout_weight attribute is the following:
Set all layout_height (or width depends on what you are doing) to fill_parent or wrap_content (it should be the same value for all elements). Try both versions. Sometimes wrap_content is better sometimes fill_parent is better.
There are many theories about the mystic of the layout_weight attribute.
This is my version^^
Edit: I forgot to mention, that the logic is inverted: the less weight the bigger the element.
I have experienced that setting the width or height (depending on which direction you want your layouts to span) to 0 can be usefull from time to time.
It is possible that you might need to use the value "0.4f", when you use values > 1.
You could also try setting the weight_sum to an amount (in your example it could be 10) and then use the layout_weight values 2, 1, 5 and 2 - but I'm not sure that it will make a difference.
Related
Why would someone use weights 2:4 instead of weights 1:2? I am looking through a Udacity course layout. It's a LinearLayout with two children views. and the children are given weights 2 and 4 respectively as opposed to 1 and 2. Why is that?
Because they wanted to? The actual values don't matter, the ratios are all that does. Usually when you see that its code that evolved over time, they originally had something in there with weight 1 and removed it (while weights don't need to be whole number, most people try to keep them that way).
I'm using several buttons in my app, but both layout_width/height "wrap_content" and "fill_parent" looks weird. The former being to small and the latter too large - both looks weird, and the former is not easy to hit with your finger.
How should I size buttons? Is it typical to define their sizes in dip? Or should I use "fill_parent" with a padding? Buttons looks weird in my app, not so in others.
That is difficult to answer in the abstract. Here are some techniques to consider:
Use android:padding="4dip" (or some other value) to make a wrap_content Button a bit bigger
Use android:textSize on the Button to make the content bigger (use some size in scaled pixels, or sp)
If you want the buttons to fill the space but divide it among themselves, use a LinearLayout, give each button a height (or width, depending if column or row) of 0px, then use android:layout_weight to allocate space between them on a percentage basis. Here is a sample project outlining this technique.
I think it is better to use fill_parent with a padding/margin instead an exact width value. So you are more flexible when the size of the parent view changes.
I'm trying to layout a set of horizontal and vertical buttons. The problem is that if I indicate 'layout_weight' for buttons, their dimensions don't follow the 'layout_width' & 'layout_height' tags.
Here's what I'm trying to achieve
So, buttons must have identical height & width and distribute evenly horizontally and vertically.
Can anyone suggest a solution please?
Thanks
UPDATE: After a lot of investigation and trying out different solutions, I came to a conclusion that my only option is to create my own custom layout and place buttons correctly there.
That configuration (the L shape) might make this a tad trickier... since the corner button might size like it's horizontal counterparts or it's vertical counterparts, depending on which linear layout they are apart of. In any event, you should be using wrap_content for the height or width, and they should all have a weight of 1. It's my understanding that the wrap_content for the height or width is important for the Linear Layout to size them correctly. It might work with fill_parent when the weights are all 1, but if you use different weights on different Views with fill_parent, things won't appear as you expect.
If you can't make it work with linearlayout and weights (which you should be able to do), you could always try to do some manual workarounds using relativelayout.
I'm using several buttons in my app, but both layout_width/height "wrap_content" and "fill_parent" looks weird. The former being to small and the latter too large - both looks weird, and the former is not easy to hit with your finger.
How should I size buttons? Is it typical to define their sizes in dip? Or should I use "fill_parent" with a padding? Buttons looks weird in my app, not so in others.
That is difficult to answer in the abstract. Here are some techniques to consider:
Use android:padding="4dip" (or some other value) to make a wrap_content Button a bit bigger
Use android:textSize on the Button to make the content bigger (use some size in scaled pixels, or sp)
If you want the buttons to fill the space but divide it among themselves, use a LinearLayout, give each button a height (or width, depending if column or row) of 0px, then use android:layout_weight to allocate space between them on a percentage basis. Here is a sample project outlining this technique.
I think it is better to use fill_parent with a padding/margin instead an exact width value. So you are more flexible when the size of the parent view changes.
I have referred to this site for Layout Tricks.
Over there one attribute is declared as android:layout_weight="1".
So my confusion is regarding android:layout_weight:
where is the exact use of android:layout_weight ?
Where should we use android:layout_weight ?
From what I understand of layout_weight, the closest comparison I could find is when you pass some % in HTML for the width or height of your div. In our context, the weigth seems to refer to the % of width or heigth your widget should get for itself compared to its neighbours in a given ViewGroup. You can find another exemple with more explanation here.
If you look the way they use it, it sticks exactly to the definition. They give a height of 0dip to keep the whole space free, and then uses equal weigths for the 2 widgets to make same th same height.
layout_weight is quite simply a priority to apply to a view class, the higher, the larger the control or group will be in proportion to it's mates inside that group. The default is 0, so a weight of 1 makes it twice as large as other default controls. No mystery.