If weightSum is not specified, Android will just add the weights of the children together. So, is there really a reason to use weightSum? Is there a situation where I shouldn't use it?
Is it more efficient than simply letting android add the weight by itself?
The important word in the reference documentation description is "single": "This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0." In this case, the sum of the weights of the children (in this case, only child) is different to the weightSum.
So you only need to use weightSum when you won't necessarily have the children filling the entire LinearLayout.
For example you can set weightSum = 3 for your layout, and weight = 1 for two children views. In result your views will range 66.6% of all place in layout.
Good luck!
Related
is PercentRelativeLayout replacement of LinearLayout weights ? probably yes.
but how can we make sure that we have optimized our app if we remove linear layouts and reduce layout hierarchy by replacing linear layout with percent relative layout and by removing weight attribute from views but add percentWidth and percentHeight instead of using weights.
we can not optimize by replacing it with percentRelative layout as because there will same number of tags for both e.g 50,50 is two parts of 100 so I think both are same in this case.
i am getting "Set android:baselineAligned="false" on this element for better performance" while using LinearLayout, I know its regarding performance,but i dont know exactly why it is,please clarify me
If you are looking for a visual explanation like me, then you might find this useful.
When baselineAlign is enabled(i.e if it is set to true), then all the text in that line will be aligned to have the same baseline.
Note: By default, baselineAligned is set to true. (i.e. baselineAligned=true)
When you make baselineAligned=false, all it needs to do is to add new elements to the linear layout and be done with it. The app need not worry about where the baseline of other elements in the layout is.
See the image below for more clarity
android:baselineAligned/setBaselineAligned(boolean): When set to false,
prevents the layout from aligning its children's baselines.
So can take example with linear layout with horizontal child views having multiple TextView with different text size or different views like button there basealignment would be different and you cannot adjust it to have same basealignment if you set it to false
Reference
Update:
By setting android:baselineAligned="false" , you're preventing the extra work your app's layout has to do in order to Align its children's baselines; which can obviously increase the performance. (Less unnecessary operations on UI => Better performance) as mentioned here
Ok so imagine a situation where you have the main layout as LinearLayout (which supports layout_weight) and inside it you have nested RelativeLayout (which does not support layout_weight).
Now since the RelativeLayout is nested inside LinearLayout, will it be able to use layout_weight ? And if the case was reversed (Linear inside Relative), will LinearLayout be able to use layout_below, layout_toParentLeft, etc ?
Now since the RelativeLayout is nested inside LinearLayout, will it be able to use layout_weight ?
No. Since RelativeLayout is nested inside LinearLayout it can be given a weight to be weighted inside the LinearLayout but it does not inherit the property of weightSum to hand out to children.
And if the case was reversed (Linear inside Relative), will LinearLayout be able to use layout_below, layout_toParentLeft, etc ?
Yes, you are inside of a RelativeLayout so any child can be given those properties. However, any child inside of that nested LinearLayout can not use the properties of RelativeLayout as in your example.
WeighSum Docs
a number greater than 0.0f, or a number lower than or equals to 0.0f
if the weight sum should be computed from the children's
layout_weight"
android:weightSUme="aNumber" can be used to "sum" the weight of the children. If all the childrens sums will add up to 1 then you don't need this property. As I recently learned from #RomainGuy through a discussion with #Squonk, it is really only needed if they won't equal 1 and you want some empty space in your layout. Otherwise, the cpu will determine what the weightSum will be.
The answer to your question is not very complicated. I can synthesize it in two letters: NO
I came through many examples in internet.I found that neither Relative Layout nor Linear Layout supports android:layout_gravity.By it I mean the views inside these layouts does not support android:layout_gravity attribute. So any one having idea which layout supports android:layout_gravity and how to use it in better way?
Children (that is, direct descendants in the View hierarchy) of LinearLayout do use layout_gravity (see LinearLayout.LayoutParams), but only on the "secondary" axis. So, in a vertical LinearLayout, center_horiztonal will work, but center_vertical will do nothing.
Children of FrameLayout also support layout_gravity (see FrameLayout.LayoutParams).
Keep in mind that the layout_* parameters set values in a LayoutParams object provided by the view parent. So a layout_* parameter will only have an effect if the parent view supports the parameter.
Actually if you use RelativeLayout you don't need to use layout_gravity.Better way to position your layout's elements are android. Here you can get a good explanation how to use RelativeLayout.
On an Android layout, I'd like to have a set of rows, each with two TextViews. The leftmost column of TextViews should be right-aligned, just left of an imaginary centerline down the screen. The rightmost column should be left-aligned.
Examples of this can be seen at http://stuff.greenberg.org/ScopeCalc.htm
What's the best layout to use?
IMO, TableLayout would be a logical choice with appropriate use of colspan/rowspan.
You can also do this using LinearLayout, with the two sub-views of each row each getting 50% of the width.
Using GridLayout you can apply columnspan and rowspan properties to the views inside the grid.