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.
Related
The linear layout in android is like this...
Layout available
But I want a layout that will move the inner widget down instead of forward like this
Layout I want
there is no layout in Android, which can do such replacing, but you can use Flexbox lib from Google
If you want to only use linear layout then you can do nesting of 2 linear layout
in 1 : orientation will be vertical, and horizontal in other
To make it more easy, use relative/constraint layout.
left one is the mobile phone and right one is the device on android studio.
Use linear layout (horizontal) inside your relative layout and add buttons to it
Set linear layout's attribute weight_sum to number of buttons in a row (for example 5)
Set buttons' attribute layout_weight instead of explicitly defining size in dp (for example 1 for each button)
Duplicate your linear layout for all the rows
Profit
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
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!
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.